1*f9581bceSthorpej /* $NetBSD: com_mca.c,v 1.23 2018/12/08 17:46:14 thorpej Exp $ */
21c341b3dSjdolecek
31c341b3dSjdolecek /*-
4ba9261baSjdolecek * Copyright (c) 2001 The NetBSD Foundation, Inc.
51c341b3dSjdolecek * All rights reserved.
61c341b3dSjdolecek *
71c341b3dSjdolecek * This code is derived from software contributed to The NetBSD Foundation
81c341b3dSjdolecek * by Charles M. Hannum.
91c341b3dSjdolecek *
101c341b3dSjdolecek * Redistribution and use in source and binary forms, with or without
111c341b3dSjdolecek * modification, are permitted provided that the following conditions
121c341b3dSjdolecek * are met:
131c341b3dSjdolecek * 1. Redistributions of source code must retain the above copyright
141c341b3dSjdolecek * notice, this list of conditions and the following disclaimer.
151c341b3dSjdolecek * 2. Redistributions in binary form must reproduce the above copyright
161c341b3dSjdolecek * notice, this list of conditions and the following disclaimer in the
171c341b3dSjdolecek * documentation and/or other materials provided with the distribution.
181c341b3dSjdolecek *
191c341b3dSjdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201c341b3dSjdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211c341b3dSjdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221c341b3dSjdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231c341b3dSjdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241c341b3dSjdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251c341b3dSjdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261c341b3dSjdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271c341b3dSjdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281c341b3dSjdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291c341b3dSjdolecek * POSSIBILITY OF SUCH DAMAGE.
301c341b3dSjdolecek */
311c341b3dSjdolecek
321c341b3dSjdolecek /*-
331c341b3dSjdolecek * Copyright (c) 1991 The Regents of the University of California.
341c341b3dSjdolecek * All rights reserved.
351c341b3dSjdolecek *
361c341b3dSjdolecek * Redistribution and use in source and binary forms, with or without
371c341b3dSjdolecek * modification, are permitted provided that the following conditions
381c341b3dSjdolecek * are met:
391c341b3dSjdolecek * 1. Redistributions of source code must retain the above copyright
401c341b3dSjdolecek * notice, this list of conditions and the following disclaimer.
411c341b3dSjdolecek * 2. Redistributions in binary form must reproduce the above copyright
421c341b3dSjdolecek * notice, this list of conditions and the following disclaimer in the
431c341b3dSjdolecek * documentation and/or other materials provided with the distribution.
44aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
451c341b3dSjdolecek * may be used to endorse or promote products derived from this software
461c341b3dSjdolecek * without specific prior written permission.
471c341b3dSjdolecek *
481c341b3dSjdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
491c341b3dSjdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
501c341b3dSjdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
511c341b3dSjdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
521c341b3dSjdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
531c341b3dSjdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
541c341b3dSjdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
551c341b3dSjdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
561c341b3dSjdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
571c341b3dSjdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
581c341b3dSjdolecek * SUCH DAMAGE.
591c341b3dSjdolecek *
601c341b3dSjdolecek * @(#)com.c 7.5 (Berkeley) 5/16/91
611c341b3dSjdolecek */
621c341b3dSjdolecek
631c341b3dSjdolecek /*
641c341b3dSjdolecek * This driver attaches serial port boards and internal modems.
651c341b3dSjdolecek */
661c341b3dSjdolecek
678b7bb912Slukem #include <sys/cdefs.h>
68*f9581bceSthorpej __KERNEL_RCSID(0, "$NetBSD: com_mca.c,v 1.23 2018/12/08 17:46:14 thorpej Exp $");
698b7bb912Slukem
701c341b3dSjdolecek #include <sys/param.h>
711c341b3dSjdolecek #include <sys/systm.h>
721c341b3dSjdolecek #include <sys/ioctl.h>
731c341b3dSjdolecek #include <sys/select.h>
741c341b3dSjdolecek #include <sys/tty.h>
751c341b3dSjdolecek #include <sys/proc.h>
761c341b3dSjdolecek #include <sys/file.h>
771c341b3dSjdolecek #include <sys/uio.h>
781c341b3dSjdolecek #include <sys/kernel.h>
791c341b3dSjdolecek #include <sys/syslog.h>
801c341b3dSjdolecek #include <sys/device.h>
811c341b3dSjdolecek
82a2a38285Sad #include <sys/intr.h>
83a2a38285Sad #include <sys/bus.h>
841c341b3dSjdolecek
851c341b3dSjdolecek #include <dev/ic/comreg.h>
861c341b3dSjdolecek #include <dev/ic/comvar.h>
871c341b3dSjdolecek
881c341b3dSjdolecek #include <dev/mca/mcavar.h>
891c341b3dSjdolecek #include <dev/mca/mcadevs.h>
901c341b3dSjdolecek
911c341b3dSjdolecek struct com_mca_softc {
921c341b3dSjdolecek struct com_softc sc_com; /* real "com" softc */
931c341b3dSjdolecek
941c341b3dSjdolecek /* MCA-specific goo. */
951c341b3dSjdolecek void *sc_ih; /* interrupt handler */
961c341b3dSjdolecek };
971c341b3dSjdolecek
98607ead0eScube int com_mca_probe(device_t, cfdata_t , void *);
99607ead0eScube void com_mca_attach(device_t, device_t, void *);
100b973e27bSjdolecek
10118db93c7Sperry static int ibm_modem_getcfg(struct mca_attach_args *, int *, int *);
10218db93c7Sperry static int neocom1_getcfg(struct mca_attach_args *, int *, int *);
10318db93c7Sperry static int ibm_mpcom_getcfg(struct mca_attach_args *, int *, int *);
1041c341b3dSjdolecek
105607ead0eScube CFATTACH_DECL_NEW(com_mca, sizeof(struct com_mca_softc),
106c9b3657cSthorpej com_mca_probe, com_mca_attach, NULL, NULL);
1071c341b3dSjdolecek
1081c341b3dSjdolecek static const struct com_mca_product {
1091c341b3dSjdolecek u_int32_t cp_prodid; /* MCA product ID */
1101c341b3dSjdolecek const char *cp_name; /* device name */
11118db93c7Sperry int (*cp_getcfg)(struct mca_attach_args *, int *iobase, int *irq);
1121c341b3dSjdolecek /* get device i/o base and irq */
1131c341b3dSjdolecek } com_mca_products[] = {
1141c341b3dSjdolecek { MCA_PRODUCT_IBM_MOD, "IBM Internal Modem", ibm_modem_getcfg },
115b973e27bSjdolecek { MCA_PRODUCT_NEOCOM1, "NeoTecH Single RS-232 Async. Adapter, SM110",
116b973e27bSjdolecek neocom1_getcfg },
117a20a1345Sjdolecek { MCA_PRODUCT_IBM_MPCOM,"IBM Multi-Protocol Communications Adapter",
118a20a1345Sjdolecek ibm_mpcom_getcfg },
1191c341b3dSjdolecek { 0, NULL, NULL },
1201c341b3dSjdolecek };
1211c341b3dSjdolecek
12218db93c7Sperry static const struct com_mca_product *com_mca_lookup(int);
1231c341b3dSjdolecek
1241c341b3dSjdolecek static const struct com_mca_product *
com_mca_lookup(int ma_id)125607ead0eScube com_mca_lookup(int ma_id)
1261c341b3dSjdolecek {
1271c341b3dSjdolecek const struct com_mca_product *cpp;
1281c341b3dSjdolecek
1291c341b3dSjdolecek for (cpp = com_mca_products; cpp->cp_name != NULL; cpp++)
1301c341b3dSjdolecek if (cpp->cp_prodid == ma_id)
1311c341b3dSjdolecek return (cpp);
1321c341b3dSjdolecek
1331c341b3dSjdolecek return (NULL);
1341c341b3dSjdolecek }
1351c341b3dSjdolecek
1361c341b3dSjdolecek int
com_mca_probe(device_t parent,cfdata_t match,void * aux)137607ead0eScube com_mca_probe(device_t parent, cfdata_t match, void *aux)
1381c341b3dSjdolecek {
1391c341b3dSjdolecek struct mca_attach_args *ma = aux;
1401c341b3dSjdolecek
1411c341b3dSjdolecek if (com_mca_lookup(ma->ma_id))
1421c341b3dSjdolecek return (1);
1431c341b3dSjdolecek
1441c341b3dSjdolecek return (0);
1451c341b3dSjdolecek }
1461c341b3dSjdolecek
1471c341b3dSjdolecek void
com_mca_attach(device_t parent,device_t self,void * aux)148607ead0eScube com_mca_attach(device_t parent, device_t self, void *aux)
1491c341b3dSjdolecek {
150838ee1e0Sthorpej struct com_mca_softc *isc = device_private(self);
1511c341b3dSjdolecek struct com_softc *sc = &isc->sc_com;
1521c341b3dSjdolecek int iobase, irq;
1531c341b3dSjdolecek struct mca_attach_args *ma = aux;
1541c341b3dSjdolecek const struct com_mca_product *cpp;
15534537908Sgdamore bus_space_handle_t ioh;
1561c341b3dSjdolecek
157607ead0eScube sc->sc_dev = self;
1581c341b3dSjdolecek cpp = com_mca_lookup(ma->ma_id);
1591c341b3dSjdolecek
1601c341b3dSjdolecek /* get iobase and irq */
161a20a1345Sjdolecek if ((*cpp->cp_getcfg)(ma, &iobase, &irq))
1621c341b3dSjdolecek return;
1631c341b3dSjdolecek
16434537908Sgdamore if (bus_space_map(ma->ma_iot, iobase, COM_NPORTS, 0, &ioh)) {
165607ead0eScube aprint_error(": can't map i/o space\n");
1661c341b3dSjdolecek return;
1671c341b3dSjdolecek }
1681c341b3dSjdolecek
169*f9581bceSthorpej com_init_regs(&sc->sc_regs, ma->ma_iot, ioh, iobase);
1701c341b3dSjdolecek sc->sc_frequency = COM_FREQ;
1711c341b3dSjdolecek
172607ead0eScube aprint_normal(" slot %d i/o %#x-%#x irq %d", ma->ma_slot + 1,
173b973e27bSjdolecek iobase, iobase + COM_NPORTS - 1, irq);
174b973e27bSjdolecek
1751c341b3dSjdolecek com_attach_subr(sc);
1761c341b3dSjdolecek
177607ead0eScube aprint_normal_dev(self, "%s\n", cpp->cp_name);
178b973e27bSjdolecek
1791c341b3dSjdolecek isc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_SERIAL,
1801c341b3dSjdolecek comintr, sc);
1811c341b3dSjdolecek if (isc->sc_ih == NULL) {
182607ead0eScube aprint_error_dev(self,
183607ead0eScube "couldn't establish interrupt handler\n");
1841c341b3dSjdolecek return;
1851c341b3dSjdolecek }
1861c341b3dSjdolecek
1871c341b3dSjdolecek /*
188e4f38925Sdyoung * com_cleanup: shutdown hook for buggy BIOSs that don't
189e4f38925Sdyoung * recognize the UART without a disabled FIFO.
1901c341b3dSjdolecek * XXX is this necessary on MCA ? --- jdolecek
1911c341b3dSjdolecek */
192e4f38925Sdyoung if (!pmf_device_register1(self, com_suspend, com_resume, com_cleanup))
193e4f38925Sdyoung aprint_error_dev(self, "could not establish shutdown hook\n");
1941c341b3dSjdolecek }
1951c341b3dSjdolecek
1961c341b3dSjdolecek /* map serial_X to iobase and irq */
1971c341b3dSjdolecek static const struct {
1981c341b3dSjdolecek int iobase;
1991c341b3dSjdolecek int irq;
2001c341b3dSjdolecek } MCA_SERIAL[] = {
2011c341b3dSjdolecek { 0x03f8, 4 }, /* SERIAL_1 */
2021c341b3dSjdolecek { 0x02f8, 3 }, /* SERIAL_2 */
2031c341b3dSjdolecek { 0x3220, 3 }, /* SERIAL_3 */
2041c341b3dSjdolecek { 0x3228, 3 }, /* SERIAL_4 */
2051c341b3dSjdolecek { 0x4220, 3 }, /* SERIAL_5 */
2061c341b3dSjdolecek { 0x4228, 3 }, /* SERIAL_6 */
2071c341b3dSjdolecek { 0x5220, 3 }, /* SERIAL_7 */
2081c341b3dSjdolecek { 0x5228, 3 }, /* SERIAL_8 */
2091c341b3dSjdolecek };
2101c341b3dSjdolecek
2111c341b3dSjdolecek /*
2121c341b3dSjdolecek * Get config for IBM Internal Modem (ID 0xEDFF). This beast doesn't
2131c341b3dSjdolecek * seem to support even AT commands, it's good as example for adding
2141c341b3dSjdolecek * other stuff though.
2151c341b3dSjdolecek */
2161c341b3dSjdolecek static int
ibm_modem_getcfg(struct mca_attach_args * ma,int * iobasep,int * irqp)217607ead0eScube ibm_modem_getcfg(struct mca_attach_args *ma, int *iobasep, int *irqp)
2181c341b3dSjdolecek {
2191c341b3dSjdolecek int pos2;
2201c341b3dSjdolecek int snum;
2211c341b3dSjdolecek
2221c341b3dSjdolecek pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
2231c341b3dSjdolecek
2241c341b3dSjdolecek /*
2251c341b3dSjdolecek * POS register 2: (adf pos0)
2261c341b3dSjdolecek * 7 6 5 4 3 2 1 0
2271c341b3dSjdolecek * \__/ \__ enable: 0=adapter disabled, 1=adapter enabled
2281c341b3dSjdolecek * \_____ Serial Configuration: XX=SERIAL_XX
2291c341b3dSjdolecek */
2301c341b3dSjdolecek
2311c341b3dSjdolecek snum = (pos2 & 0x0e) >> 1;
2321c341b3dSjdolecek
2331c341b3dSjdolecek *iobasep = MCA_SERIAL[snum].iobase;
2341c341b3dSjdolecek *irqp = MCA_SERIAL[snum].irq;
2351c341b3dSjdolecek
2361c341b3dSjdolecek return (0);
2371c341b3dSjdolecek }
238b973e27bSjdolecek
239b973e27bSjdolecek /*
240b973e27bSjdolecek * Get configuration for NeoTecH Single RS-232 Async. Adapter, SM110.
241b973e27bSjdolecek */
242b973e27bSjdolecek static int
neocom1_getcfg(struct mca_attach_args * ma,int * iobasep,int * irqp)243607ead0eScube neocom1_getcfg(struct mca_attach_args *ma, int *iobasep, int *irqp)
244b973e27bSjdolecek {
245b973e27bSjdolecek int pos2, pos3, pos4;
246b973e27bSjdolecek static const int neotech_irq[] = { 12, 9, 4, 3 };
247b973e27bSjdolecek
248b973e27bSjdolecek pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
249b973e27bSjdolecek pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
250b973e27bSjdolecek pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
251b973e27bSjdolecek
252b973e27bSjdolecek /*
253b973e27bSjdolecek * POS register 2: (adf pos0)
254b973e27bSjdolecek * 7 6 5 4 3 2 1 0
255b973e27bSjdolecek * 1 \_/ \__ enable: 0=adapter disabled, 1=adapter enabled
256b973e27bSjdolecek * \____ IRQ: 11=3 10=4 01=9 00=12
257b973e27bSjdolecek *
258b973e27bSjdolecek * POS register 3: (adf pos1)
259b973e27bSjdolecek * 7 6 5 4 3 2 1 0
260b973e27bSjdolecek * \______/
261b973e27bSjdolecek * \_________ I/O Address: bits 7-3
262b973e27bSjdolecek *
263b973e27bSjdolecek * POS register 4: (adf pos2)
264b973e27bSjdolecek * 7 6 5 4 3 2 1 0
265b973e27bSjdolecek * \_____________/
266b973e27bSjdolecek * \__ I/O Address: bits 15-8
267b973e27bSjdolecek */
268b973e27bSjdolecek
269b973e27bSjdolecek *iobasep = (pos4 << 8) | (pos3 & 0xf8);
270b973e27bSjdolecek *irqp = neotech_irq[(pos2 & 0x06) >> 1];
271b973e27bSjdolecek
272b973e27bSjdolecek return (0);
273b973e27bSjdolecek }
274a20a1345Sjdolecek
275a20a1345Sjdolecek /*
276a20a1345Sjdolecek * Get configuration for IBM Multi-Protocol Communications Adapter.
277a20a1345Sjdolecek * We only support SERIAL mode, bail out if set to SDLC or BISYNC.
278a20a1345Sjdolecek */
279a20a1345Sjdolecek static int
ibm_mpcom_getcfg(struct mca_attach_args * ma,int * iobasep,int * irqp)280607ead0eScube ibm_mpcom_getcfg(struct mca_attach_args *ma, int *iobasep, int *irqp)
281a20a1345Sjdolecek {
282a20a1345Sjdolecek int snum, pos2;
283a20a1345Sjdolecek
284a20a1345Sjdolecek pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
285a20a1345Sjdolecek
286a20a1345Sjdolecek /*
287a20a1345Sjdolecek * For SERIAL mode, bit 4 has to be 0.
288a20a1345Sjdolecek *
289a20a1345Sjdolecek * POS register 2: (adf pos0)
290a20a1345Sjdolecek * 7 6 5 4 3 2 1 0
291a20a1345Sjdolecek * 0 \__/ \__ enable: 0=adapter disabled, 1=adapter enabled
292a20a1345Sjdolecek * \_____ Serial Configuration: XX=SERIAL_XX
293a20a1345Sjdolecek */
294a20a1345Sjdolecek
295a20a1345Sjdolecek if (pos2 & 0x10) {
296607ead0eScube aprint_error(": not set to SERIAL mode, ignored\n");
297a20a1345Sjdolecek return (1);
298a20a1345Sjdolecek }
299a20a1345Sjdolecek
300a20a1345Sjdolecek snum = (pos2 & 0x0e) >> 1;
301a20a1345Sjdolecek
302a20a1345Sjdolecek *iobasep = MCA_SERIAL[snum].iobase;
303a20a1345Sjdolecek *irqp = MCA_SERIAL[snum].irq;
304a20a1345Sjdolecek
305a20a1345Sjdolecek return (0);
306a20a1345Sjdolecek }
307