11.3Sskrll/*	$NetBSD: com_ssio.c,v 1.3 2019/04/15 20:40:37 skrll Exp $	*/
21.1Sskrll
31.1Sskrll/*	$OpenBSD: com_ssio.c,v 1.2 2007/06/24 16:28:39 kettenis Exp $	*/
41.1Sskrll
51.1Sskrll/*
61.1Sskrll * Copyright (c) 2007 Mark Kettenis
71.1Sskrll *
81.1Sskrll * Permission to use, copy, modify, and distribute this software for any
91.1Sskrll * purpose with or without fee is hereby granted, provided that the above
101.1Sskrll * copyright notice and this permission notice appear in all copies.
111.1Sskrll *
121.1Sskrll * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
131.1Sskrll * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
141.1Sskrll * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
151.1Sskrll * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
161.1Sskrll * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
171.1Sskrll * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
181.1Sskrll * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
191.1Sskrll */
201.1Sskrll
211.1Sskrll#include <sys/param.h>
221.1Sskrll#include <sys/systm.h>
231.1Sskrll#include <sys/device.h>
241.1Sskrll#include <sys/tty.h>
251.1Sskrll
261.1Sskrll#include <sys/bus.h>
271.1Sskrll#include <machine/iomod.h>
281.1Sskrll
291.1Sskrll#include <dev/ic/comreg.h>
301.1Sskrll#include <dev/ic/comvar.h>
311.1Sskrll
321.1Sskrll#include <hppa/hppa/machdep.h>
331.1Sskrll#include <hppa/dev/ssiovar.h>
341.1Sskrll
351.1Sskrllvoid *ssio_intr_establish(int, int, int (*)(void *), void *,
361.1Sskrll    const char *);
371.1Sskrll
381.1Sskrll#define COM_SSIO_FREQ	1843200
391.1Sskrll
401.1Sskrllstruct com_ssio_softc {
411.1Sskrll	struct	com_softc sc_com;	/* real "com" softc */
421.1Sskrll	void	*sc_ih;			/* interrupt handler */
431.1Sskrll};
441.1Sskrll
451.1Sskrllint     com_ssio_match(device_t, cfdata_t, void *);
461.1Sskrllvoid    com_ssio_attach(device_t, device_t, void *);
471.1Sskrll
481.1SskrllCFATTACH_DECL_NEW(com_ssio, sizeof(struct com_ssio_softc), com_ssio_match,
491.1Sskrll    com_ssio_attach, NULL, NULL);
501.1Sskrll
511.1Sskrllint
521.1Sskrllcom_ssio_match(device_t parent, cfdata_t match, void *aux)
531.1Sskrll{
541.1Sskrll	cfdata_t cf = match;
551.1Sskrll	struct ssio_attach_args *saa = aux;
561.1Sskrll
571.1Sskrll	if (strcmp(saa->saa_name, "com") != 0)
581.1Sskrll		return (0);
591.1Sskrll
601.1Sskrll	/* Check locators. */
611.1Sskrll	if (cf->ssiocf_irq != SSIO_UNK_IRQ && cf->ssiocf_irq != saa->saa_irq)
621.1Sskrll		return (0);
631.1Sskrll
641.1Sskrll	return (1);
651.1Sskrll}
661.1Sskrll
671.1Sskrllvoid
681.1Sskrllcom_ssio_attach(device_t parent, device_t self, void *aux)
691.1Sskrll{
701.1Sskrll	struct com_ssio_softc *sc_ssio = device_private(self);
711.1Sskrll	struct com_softc *sc = &sc_ssio->sc_com;
721.1Sskrll	struct ssio_attach_args *saa = aux;
731.1Sskrll	int pagezero_cookie;
741.1Sskrll
751.1Sskrll	bus_addr_t iobase;
761.1Sskrll	bus_space_handle_t ioh;
771.1Sskrll	bus_space_tag_t iot;
781.1Sskrll
791.1Sskrll	sc->sc_dev = self;
801.1Sskrll	iobase = saa->saa_iobase;
811.1Sskrll	iot = saa->saa_iot;
821.1Sskrll	if (bus_space_map(iot, iobase, COM_NPORTS,
831.1Sskrll	    0, &ioh)) {
841.1Sskrll		aprint_error(": can't map I/O space\n");
851.1Sskrll		return;
861.1Sskrll	}
871.1Sskrll
881.1Sskrll        /* Test if this is the console. */
891.1Sskrll	pagezero_cookie = hppa_pagezero_map();
901.1Sskrll	if (PAGE0->mem_cons.pz_class == PCL_DUPLEX &&
911.1Sskrll	    PAGE0->mem_cons.pz_hpa == (struct iomod *)ioh) {
921.1Sskrll		bus_space_unmap(iot, ioh, COM_NPORTS);
931.1Sskrll		if (comcnattach(iot, iobase, B9600, COM_SSIO_FREQ,
941.3Sskrll		    COM_TYPE_NORMAL,
951.1Sskrll		    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0) {
961.1Sskrll			aprint_error(": can't comcnattach\n");
971.1Sskrll			hppa_pagezero_unmap(pagezero_cookie);
981.1Sskrll			return;
991.1Sskrll		}
1001.1Sskrll	}
1011.1Sskrll	hppa_pagezero_unmap(pagezero_cookie);
1021.1Sskrll
1031.1Sskrll	sc->sc_frequency = COM_SSIO_FREQ;
1041.2Sthorpej	com_init_regs(&sc->sc_regs, iot, ioh, iobase);
1051.1Sskrll	com_attach_subr(sc);
1061.1Sskrll
1071.1Sskrll	sc_ssio->sc_ih = ssio_intr_establish(IPL_TTY, saa->saa_irq,
1081.1Sskrll	    comintr, sc, device_xname(self));
1091.1Sskrll}
110