1/*	$OpenBSD: com_dino.c,v 1.4 2007/07/15 19:25:49 kettenis Exp $	*/
2
3/*
4 * Copyright (c) 2004 Michael Shalayeff
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/device.h>
32#include <sys/tty.h>
33
34#include <sys/bus.h>
35#include <machine/intr.h>
36#include <machine/iomod.h>
37#include <machine/autoconf.h>
38
39#include <dev/ic/comreg.h>
40#include <dev/ic/comvar.h>
41
42#include <hp700/dev/cpudevs.h>
43#include <hp700/hp700/machdep.h>
44
45void *dino_intr_establish(void *sc, int irq, int pri,
46    int (*handler)(void *v), void *arg);
47
48#define	COM_DINO_FREQ	7272700
49
50struct com_dino_softc {
51	struct  com_softc sc_com;	/* real "com" softc */
52	void	*sc_ih;			/* interrupt handler */
53};
54
55struct com_dino_regs {
56	uint8_t		reset;
57	uint8_t		pad0[3];
58	uint8_t		test;
59#define	COM_DINO_PAR_LOOP	0x01
60#define	COM_DINO_CLK_SEL	0x02
61	uint8_t		pad1[3];
62	uint32_t	iodc;
63	uint8_t		pad2[0x54];
64	uint8_t		dither;
65};
66
67int	com_dino_match(device_t, cfdata_t, void *);
68void	com_dino_attach(device_t, device_t, void *);
69
70CFATTACH_DECL_NEW(com_dino, sizeof(struct com_dino_softc), com_dino_match,
71    com_dino_attach, NULL, NULL);
72
73int
74com_dino_match(device_t parent, cfdata_t match, void *aux)
75{
76	struct confargs *ca = aux;
77
78	if (ca->ca_type.iodc_type != HPPA_TYPE_FIO ||
79	    ca->ca_type.iodc_sv_model != HPPA_FIO_GRS232)
80		return (0);
81
82	return (1);
83	/* HOZER comprobe1(ca->ca_iot, ca->ca_hpa + IOMOD_DEVOFFSET); */
84}
85
86void
87com_dino_attach(device_t parent, device_t self, void *aux)
88{
89	void *sc_dino = device_private(parent);
90	struct com_dino_softc *sc_comdino = device_private(self);
91	struct com_softc *sc = &sc_comdino->sc_com;
92	struct confargs *ca = aux;
93	struct com_dino_regs *regs = (struct com_dino_regs *)ca->ca_hpa;
94	int pagezero_cookie;
95
96	bus_addr_t iobase;
97	bus_space_handle_t ioh;
98
99	sc->sc_dev = self;
100	iobase = (bus_addr_t)ca->ca_hpa + IOMOD_DEVOFFSET;
101	sc->sc_frequency = COM_DINO_FREQ;
102
103	/* Test if this is the console.  Compare either HPA or device path. */
104	pagezero_cookie = hp700_pagezero_map();
105	if (PAGE0->mem_cons.pz_class == PCL_DUPLEX &&
106	    PAGE0->mem_cons.pz_hpa == (struct iomod *)ca->ca_hpa) {
107
108		/*
109		 * This port is the console.  In this case we must call
110		 * comcnattach() and later com_is_console() to initialize
111		 * everything properly.
112		 */
113
114		if (comcnattach(ca->ca_iot, iobase, B9600,
115		    sc->sc_frequency, COM_TYPE_NORMAL,
116		    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0) {
117			aprint_error(": can't comcnattach\n");
118			hp700_pagezero_unmap(pagezero_cookie);
119			return;
120		}
121	}
122	hp700_pagezero_unmap(pagezero_cookie);
123
124	/*
125	 * Get the already initialized console ioh via com_is_console() if
126	 * this is the console or map the I/O space if this isn't the console.
127	 */
128
129	if (!com_is_console(ca->ca_iot, iobase, &ioh) &&
130	    bus_space_map(ca->ca_iot, iobase, COM_NPORTS, 0, &ioh) != 0) {
131		aprint_error(": can't map I/O space\n");
132		return;
133	}
134	COM_INIT_REGS(sc->sc_regs, ca->ca_iot, ioh, iobase);
135
136	/* select clock freq */
137	regs->test = COM_DINO_CLK_SEL;
138
139	com_attach_subr(sc);
140
141	ca->ca_irq = 10;
142
143	sc_comdino->sc_ih = dino_intr_establish(sc_dino, ca->ca_irq, IPL_TTY,
144	    comintr, sc);
145}
146