1*6d487047Sthorpej /* $NetBSD: com_hpcio.c,v 1.12 2018/12/08 17:46:11 thorpej Exp $ */
201b846e2Stakemura
301b846e2Stakemura /*-
401b846e2Stakemura * Copyright (c) 2002 TAKEMRUA Shin. All rights reserved.
501b846e2Stakemura *
601b846e2Stakemura * Redistribution and use in source and binary forms, with or without
701b846e2Stakemura * modification, are permitted provided that the following conditions
801b846e2Stakemura * are met:
901b846e2Stakemura * 1. Redistributions of source code must retain the above copyright
1001b846e2Stakemura * notice, this list of conditions and the following disclaimer.
1101b846e2Stakemura * 2. Redistributions in binary form must reproduce the above copyright
1201b846e2Stakemura * notice, this list of conditions and the following disclaimer in the
1301b846e2Stakemura * documentation and/or other materials provided with the distribution.
1401b846e2Stakemura * 3. Neither the name of the project nor the names of its contributors
1501b846e2Stakemura * may be used to endorse or promote products derived from this software
1601b846e2Stakemura * without specific prior written permission.
1701b846e2Stakemura *
1801b846e2Stakemura * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1901b846e2Stakemura * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2001b846e2Stakemura * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2101b846e2Stakemura * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2201b846e2Stakemura * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2301b846e2Stakemura * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2401b846e2Stakemura * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2501b846e2Stakemura * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2601b846e2Stakemura * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2701b846e2Stakemura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2801b846e2Stakemura * SUCH DAMAGE.
2901b846e2Stakemura *
3001b846e2Stakemura */
3101b846e2Stakemura
320c82163cSlukem #include <sys/cdefs.h>
33*6d487047Sthorpej __KERNEL_RCSID(0, "$NetBSD: com_hpcio.c,v 1.12 2018/12/08 17:46:11 thorpej Exp $");
340c82163cSlukem
3501b846e2Stakemura #include "opt_kgdb.h"
3601b846e2Stakemura
3701b846e2Stakemura #include <sys/param.h>
3801b846e2Stakemura #include <sys/systm.h>
3901b846e2Stakemura #include <sys/device.h>
4001b846e2Stakemura #include <sys/reboot.h>
4101b846e2Stakemura #include <sys/termios.h>
4201b846e2Stakemura
4301b846e2Stakemura #include <machine/intr.h>
4401b846e2Stakemura #include <machine/bus.h>
4501b846e2Stakemura #include <machine/platid.h>
4601b846e2Stakemura #include <machine/platid_mask.h>
4701b846e2Stakemura
4801b846e2Stakemura #include <hpcmips/dev/com_hpciovar.h>
4901b846e2Stakemura
5001b846e2Stakemura #include <dev/hpc/hpciovar.h>
5101b846e2Stakemura #include <dev/ic/comvar.h>
5201b846e2Stakemura #include <dev/ic/comreg.h>
5301b846e2Stakemura
5401b846e2Stakemura #include "locators.h"
5501b846e2Stakemura
5601b846e2Stakemura #define COM_HPCIODEBUG
5701b846e2Stakemura #ifdef COM_HPCIODEBUG
5801b846e2Stakemura int com_hpcio_debug = 0;
5901b846e2Stakemura #define DPRINTF(arg...) do { if (com_hpcio_debug) printf(arg); } while (0)
6001b846e2Stakemura #else
6101b846e2Stakemura #define DPRINTF(arg...) do {} while (0)
6201b846e2Stakemura #endif
6301b846e2Stakemura
6434537908Sgdamore /*
6534537908Sgdamore * XXX: NOTE: With the new com(4) COM_REGMAP support, the custom bus_space
6634537908Sgdamore * in this file could/should probably be removed and replaced with a custom
6734537908Sgdamore * map. Resulting in smaller/cleaner code. An hpcmips hacker should fix
6834537908Sgdamore * this, since I don't have h/w to test with -- I'm taking the route of least
6934537908Sgdamore * risk. - gdamore
7034537908Sgdamore */
7134537908Sgdamore
7201b846e2Stakemura #define COM_HPCIO_BYTE_ALIGNMENT 0
7301b846e2Stakemura #define COM_HPCIO_HALFWORD_ALIGNMENT 1
7401b846e2Stakemura
7501b846e2Stakemura struct com_hpcio_softc {
7601b846e2Stakemura struct com_softc hsc_com;
7701b846e2Stakemura struct bus_space_tag hsc_iot;
7801b846e2Stakemura struct hpcio_chip *hsc_hc;
7901b846e2Stakemura };
8001b846e2Stakemura static struct bus_space_tag com_hpcio_cniotx;
8101b846e2Stakemura static bus_space_tag_t com_hpcio_cniot = &com_hpcio_cniotx;
8201b846e2Stakemura static int com_hpcio_cniobase;
8301b846e2Stakemura
84607ead0eScube static int com_hpcio_probe(device_t, cfdata_t , void *);
85607ead0eScube static void com_hpcio_attach(device_t, device_t, void *);
8601b846e2Stakemura static int com_hpcio_common_probe(bus_space_tag_t, int, int *);
8701b846e2Stakemura void com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base);
8801b846e2Stakemura bus_space_protos(bs_notimpl);
8901b846e2Stakemura bus_space_protos(bs_through);
9001b846e2Stakemura bus_space_protos(com_hpcio);
9101b846e2Stakemura
92607ead0eScube CFATTACH_DECL_NEW(com_hpcio, sizeof(struct com_hpcio_softc),
93c5e91d44Sthorpej com_hpcio_probe, com_hpcio_attach, NULL, NULL);
9401b846e2Stakemura
9501b846e2Stakemura struct bus_space_ops com_hpcio_bs_ops = {
9601b846e2Stakemura /* mapping/unmapping */
9701b846e2Stakemura bs_through_bs_map,
9801b846e2Stakemura bs_through_bs_unmap,
9901b846e2Stakemura bs_through_bs_subregion,
10001b846e2Stakemura
10101b846e2Stakemura /* allocation/deallocation */
10201b846e2Stakemura bs_through_bs_alloc,
10301b846e2Stakemura bs_through_bs_free,
10401b846e2Stakemura
10501b846e2Stakemura /* get kernel virtual address */
10601b846e2Stakemura bs_through_bs_vaddr, /* there is no linear mapping */
10701b846e2Stakemura
10801b846e2Stakemura /* Mmap bus space for user */
10901b846e2Stakemura bs_through_bs_mmap,
11001b846e2Stakemura
11101b846e2Stakemura /* barrier */
11201b846e2Stakemura bs_through_bs_barrier,
11301b846e2Stakemura
114e63e3c0eStakemura /* probe */
115e63e3c0eStakemura bs_through_bs_peek,
116e63e3c0eStakemura bs_through_bs_poke,
117e63e3c0eStakemura
11801b846e2Stakemura /* read (single) */
11901b846e2Stakemura com_hpcio_bs_r_1,
12001b846e2Stakemura bs_notimpl_bs_r_2,
12101b846e2Stakemura bs_notimpl_bs_r_4,
12201b846e2Stakemura bs_notimpl_bs_r_8,
12301b846e2Stakemura
12401b846e2Stakemura /* read multiple */
12501b846e2Stakemura bs_notimpl_bs_rm_1,
12601b846e2Stakemura bs_notimpl_bs_rm_2,
12701b846e2Stakemura bs_notimpl_bs_rm_4,
12801b846e2Stakemura bs_notimpl_bs_rm_8,
12901b846e2Stakemura
13001b846e2Stakemura /* read region */
13101b846e2Stakemura bs_notimpl_bs_rr_1,
13201b846e2Stakemura bs_notimpl_bs_rr_2,
13301b846e2Stakemura bs_notimpl_bs_rr_4,
13401b846e2Stakemura bs_notimpl_bs_rr_8,
13501b846e2Stakemura
13601b846e2Stakemura /* write (single) */
13701b846e2Stakemura com_hpcio_bs_w_1,
13801b846e2Stakemura bs_notimpl_bs_w_2,
13901b846e2Stakemura bs_notimpl_bs_w_4,
14001b846e2Stakemura bs_notimpl_bs_w_8,
14101b846e2Stakemura
14201b846e2Stakemura /* write multiple */
14301b846e2Stakemura com_hpcio_bs_wm_1,
14401b846e2Stakemura bs_notimpl_bs_wm_2,
14501b846e2Stakemura bs_notimpl_bs_wm_4,
14601b846e2Stakemura bs_notimpl_bs_wm_8,
14701b846e2Stakemura
14801b846e2Stakemura /* write region */
14901b846e2Stakemura bs_notimpl_bs_wr_1,
15001b846e2Stakemura bs_notimpl_bs_wr_2,
15101b846e2Stakemura bs_notimpl_bs_wr_4,
15201b846e2Stakemura bs_notimpl_bs_wr_8,
15301b846e2Stakemura
15401b846e2Stakemura #ifdef BUS_SPACE_HAS_REAL_STREAM_METHODS
15501b846e2Stakemura /* read stream (single) */
15601b846e2Stakemura bs_notimpl_bs_rs_1,
15701b846e2Stakemura bs_notimpl_bs_rs_2,
15801b846e2Stakemura bs_notimpl_bs_rs_4,
15901b846e2Stakemura bs_notimpl_bs_rs_8,
16001b846e2Stakemura
16101b846e2Stakemura /* read multiple stream */
16201b846e2Stakemura bs_notimpl_bs_rms_1,
16301b846e2Stakemura bs_notimpl_bs_rms_2,
16401b846e2Stakemura bs_notimpl_bs_rms_4,
16501b846e2Stakemura bs_notimpl_bs_rms_8,
16601b846e2Stakemura
16701b846e2Stakemura /* read region stream */
16801b846e2Stakemura bs_notimpl_bs_rrs_1,
16901b846e2Stakemura bs_notimpl_bs_rrs_2,
17001b846e2Stakemura bs_notimpl_bs_rrs_4,
17101b846e2Stakemura bs_notimpl_bs_rrs_8,
17201b846e2Stakemura
17301b846e2Stakemura /* write stream (single) */
17401b846e2Stakemura bs_notimpl_bs_ws_1,
17501b846e2Stakemura bs_notimpl_bs_ws_2,
17601b846e2Stakemura bs_notimpl_bs_ws_4,
17701b846e2Stakemura bs_notimpl_bs_ws_8,
17801b846e2Stakemura
17901b846e2Stakemura /* write multiple stream */
18001b846e2Stakemura bs_notimpl_bs_wms_1,
18101b846e2Stakemura bs_notimpl_bs_wms_2,
18201b846e2Stakemura bs_notimpl_bs_wms_4,
18301b846e2Stakemura bs_notimpl_bs_wms_8,
18401b846e2Stakemura
18501b846e2Stakemura /* write region stream */
18601b846e2Stakemura bs_notimpl_bs_wrs_1,
18701b846e2Stakemura bs_notimpl_bs_wrs_2,
18801b846e2Stakemura bs_notimpl_bs_wrs_4,
18901b846e2Stakemura bs_notimpl_bs_wrs_8,
19001b846e2Stakemura #endif /* BUS_SPACE_HAS_REAL_STREAM_METHODS */
19101b846e2Stakemura
19201b846e2Stakemura /* set multi */
19301b846e2Stakemura bs_notimpl_bs_sm_1,
19401b846e2Stakemura bs_notimpl_bs_sm_2,
19501b846e2Stakemura bs_notimpl_bs_sm_4,
19601b846e2Stakemura bs_notimpl_bs_sm_8,
19701b846e2Stakemura
19801b846e2Stakemura /* set region */
19901b846e2Stakemura bs_notimpl_bs_sr_1,
20001b846e2Stakemura bs_notimpl_bs_sr_2,
20101b846e2Stakemura bs_notimpl_bs_sr_4,
20201b846e2Stakemura bs_notimpl_bs_sr_8,
20301b846e2Stakemura
20401b846e2Stakemura /* copy */
20501b846e2Stakemura bs_notimpl_bs_c_1,
20601b846e2Stakemura bs_notimpl_bs_c_2,
20701b846e2Stakemura bs_notimpl_bs_c_4,
20801b846e2Stakemura bs_notimpl_bs_c_8,
20901b846e2Stakemura };
21001b846e2Stakemura
21101b846e2Stakemura int
com_hpcio_cndb_attach(bus_space_tag_t iot,int iobase,int rate,int frequency,tcflag_t cflag,int kgdb)21201b846e2Stakemura com_hpcio_cndb_attach(bus_space_tag_t iot, int iobase, int rate,
21301b846e2Stakemura int frequency, tcflag_t cflag, int kgdb)
21401b846e2Stakemura {
21501b846e2Stakemura int alignment;
21601b846e2Stakemura
21701b846e2Stakemura DPRINTF("com_hpcio_cndb_attach()\n");
21801b846e2Stakemura if (!com_hpcio_common_probe(iot, iobase, &alignment)) {
21901b846e2Stakemura DPRINTF("com_hpcio_cndb_attach(): probe failed\n");
22001b846e2Stakemura return (ENOTTY);
22101b846e2Stakemura }
22201b846e2Stakemura if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) {
22301b846e2Stakemura DPRINTF("com_hpcio_cndb_attach(): half word aligned\n");
22401b846e2Stakemura com_hpcio_iot_init(&com_hpcio_cniotx, iot);
22501b846e2Stakemura com_hpcio_cniot = &com_hpcio_cniotx;
22601b846e2Stakemura } else {
22701b846e2Stakemura com_hpcio_cniot = iot;
22801b846e2Stakemura }
22901b846e2Stakemura com_hpcio_cniobase = iobase;
23001b846e2Stakemura DPRINTF("com_hpcio_cndb_attach(): probe succeeded\n");
23101b846e2Stakemura #ifdef KGDB
23201b846e2Stakemura if (kgdb)
23301b846e2Stakemura return (com_kgdb_attach(com_hpcio_cniot, iobase, rate,
2340eff6718Sthorpej frequency, COM_TYPE_NORMAL, cflag));
23501b846e2Stakemura else
23601b846e2Stakemura #endif
23701b846e2Stakemura return (comcnattach(com_hpcio_cniot, iobase, rate,
2380eff6718Sthorpej frequency, COM_TYPE_NORMAL, cflag));
23901b846e2Stakemura }
24001b846e2Stakemura
24101b846e2Stakemura static int
com_hpcio_common_probe(bus_space_tag_t iot,int iobase,int * alignment)24201b846e2Stakemura com_hpcio_common_probe(bus_space_tag_t iot, int iobase, int *alignment)
24301b846e2Stakemura {
24401b846e2Stakemura bus_space_handle_t ioh;
24501b846e2Stakemura static struct bus_space_tag tmpiot;
24601b846e2Stakemura int rv;
24701b846e2Stakemura
24801b846e2Stakemura /*
24901b846e2Stakemura * try byte aligned register
25001b846e2Stakemura */
25101b846e2Stakemura *alignment = COM_HPCIO_BYTE_ALIGNMENT;
25292f7bd93Stakemura if (bus_space_map(iot, iobase, 1, 0, &ioh))
25301b846e2Stakemura return 0;
25401b846e2Stakemura rv = comprobe1(iot, ioh);
25501b846e2Stakemura bus_space_unmap(iot, ioh, 1);
25601b846e2Stakemura
25701b846e2Stakemura if (rv != 0)
25801b846e2Stakemura return (rv);
25901b846e2Stakemura
26001b846e2Stakemura /*
26101b846e2Stakemura * try half word aligned register
26201b846e2Stakemura */
26301b846e2Stakemura *alignment = COM_HPCIO_HALFWORD_ALIGNMENT;
26401b846e2Stakemura com_hpcio_iot_init(&tmpiot, iot);
26592f7bd93Stakemura if (bus_space_map(&tmpiot, iobase, 1, 0, &ioh))
26601b846e2Stakemura return 0;
26701b846e2Stakemura rv = comprobe1(&tmpiot, ioh);
26801b846e2Stakemura bus_space_unmap(&tmpiot, ioh, 1);
26901b846e2Stakemura
27001b846e2Stakemura return (rv);
27101b846e2Stakemura }
27201b846e2Stakemura
27301b846e2Stakemura static int
com_hpcio_probe(device_t parent,cfdata_t cf,void * aux)274607ead0eScube com_hpcio_probe(device_t parent, cfdata_t cf, void *aux)
27501b846e2Stakemura {
27601b846e2Stakemura struct hpcio_attach_args *haa = aux;
27701b846e2Stakemura bus_space_tag_t iot = haa->haa_iot;
27801b846e2Stakemura int addr, alignment;
27901b846e2Stakemura
28001b846e2Stakemura if (cf->cf_loc[HPCIOIFCF_PLATFORM] != HPCIOIFCF_PLATFORM_DEFAULT) {
28101b846e2Stakemura platid_mask_t mask;
28201b846e2Stakemura
28301b846e2Stakemura mask = PLATID_DEREF(cf->cf_loc[HPCIOIFCF_PLATFORM]);
28401b846e2Stakemura if (!platid_match(&platid, &mask))
28501b846e2Stakemura return (0); /* platform id didn't match */
28601b846e2Stakemura }
28701b846e2Stakemura
28801b846e2Stakemura if ((addr = cf->cf_loc[HPCIOIFCF_ADDR]) == HPCIOIFCF_ADDR_DEFAULT)
28901b846e2Stakemura return (0); /* address wasn't specified */
29001b846e2Stakemura
29101b846e2Stakemura return com_hpcio_common_probe(iot, addr, &alignment);
29201b846e2Stakemura }
29301b846e2Stakemura
29401b846e2Stakemura
29501b846e2Stakemura static void
com_hpcio_attach(device_t parent,device_t self,void * aux)296607ead0eScube com_hpcio_attach(device_t parent, device_t self, void *aux)
29701b846e2Stakemura {
298607ead0eScube struct com_hpcio_softc *hsc = device_private(self);
29901b846e2Stakemura struct com_softc *sc = &hsc->hsc_com;
30001b846e2Stakemura struct hpcio_attach_args *haa = aux;
30101b846e2Stakemura bus_space_tag_t iot;
30201b846e2Stakemura bus_space_handle_t ioh;
30301b846e2Stakemura int addr, port, mode, alignment, *loc;
30401b846e2Stakemura
305607ead0eScube sc->sc_dev = self;
306607ead0eScube loc = device_cfdata(sc->sc_dev)->cf_loc;
30701b846e2Stakemura addr = loc[HPCIOIFCF_ADDR];
308607ead0eScube aprint_normal(" addr %x", addr);
30901b846e2Stakemura if ((com_hpcio_cniot == haa->haa_iot ||
31001b846e2Stakemura com_hpcio_cniot->bs_base == haa->haa_iot) &&
31101b846e2Stakemura com_hpcio_cniobase == addr &&
31201b846e2Stakemura com_is_console(com_hpcio_cniot, addr, 0)) {
31301b846e2Stakemura iot = com_hpcio_cniot;
31401b846e2Stakemura if (com_hpcio_cniot->bs_base == haa->haa_iot)
315607ead0eScube aprint_normal(", half word aligned");
31601b846e2Stakemura } else {
31701b846e2Stakemura com_hpcio_common_probe(haa->haa_iot, addr, &alignment);
31801b846e2Stakemura if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) {
319607ead0eScube aprint_normal(", half word aligned");
32001b846e2Stakemura iot = &hsc->hsc_iot;
32101b846e2Stakemura com_hpcio_iot_init(iot, haa->haa_iot);
32201b846e2Stakemura } else {
32301b846e2Stakemura iot = haa->haa_iot;
32401b846e2Stakemura }
32501b846e2Stakemura }
32601b846e2Stakemura if (bus_space_map(iot, addr, 1, 0, &ioh)) {
327607ead0eScube aprint_error(": can't map bus space\n");
32801b846e2Stakemura return;
32901b846e2Stakemura }
330*6d487047Sthorpej com_init_regs(&sc->sc_regs, iot, ioh, addr);
33101b846e2Stakemura
33201b846e2Stakemura sc->enable = NULL;
33301b846e2Stakemura sc->disable = NULL;
33401b846e2Stakemura
33501b846e2Stakemura sc->sc_frequency = COM_FREQ;
33601b846e2Stakemura com_attach_subr(sc);
33701b846e2Stakemura
33801b846e2Stakemura hsc->hsc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
33901b846e2Stakemura port = loc[HPCIOIFCF_PORT];
34001b846e2Stakemura mode = HPCIO_INTR_LEVEL | HPCIO_INTR_HIGH;
34101b846e2Stakemura hpcio_intr_establish(hsc->hsc_hc, port, mode, comintr, sc);
34201b846e2Stakemura }
34301b846e2Stakemura
34401b846e2Stakemura /*
34501b846e2Stakemura * bus stuff (registershalf word aligned)
34601b846e2Stakemura */
34701b846e2Stakemura void
com_hpcio_iot_init(bus_space_tag_t iot,bus_space_tag_t base)34801b846e2Stakemura com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base)
34901b846e2Stakemura {
35001b846e2Stakemura
35101b846e2Stakemura iot->bs_base = base;
35201b846e2Stakemura iot->bs_ops = com_hpcio_bs_ops; /* structure assignment */
35301b846e2Stakemura iot->bs_ops.bs_r_1 = com_hpcio_bs_r_1;
35401b846e2Stakemura iot->bs_ops.bs_w_1 = com_hpcio_bs_w_1;
35501b846e2Stakemura iot->bs_ops.bs_wm_1 = com_hpcio_bs_wm_1;
35601b846e2Stakemura }
35701b846e2Stakemura
35801b846e2Stakemura u_int8_t
com_hpcio_bs_r_1(bus_space_tag_t t,bus_space_handle_t bsh,bus_size_t offset)35901b846e2Stakemura com_hpcio_bs_r_1(bus_space_tag_t t, bus_space_handle_t bsh,
36001b846e2Stakemura bus_size_t offset)
36101b846e2Stakemura {
36201b846e2Stakemura return bus_space_read_1(t->bs_base, bsh, offset * 2);
36301b846e2Stakemura }
36401b846e2Stakemura
36501b846e2Stakemura void
com_hpcio_bs_w_1(bus_space_tag_t t,bus_space_handle_t bsh,bus_size_t offset,u_int8_t value)36601b846e2Stakemura com_hpcio_bs_w_1(bus_space_tag_t t, bus_space_handle_t bsh,
36701b846e2Stakemura bus_size_t offset, u_int8_t value)
36801b846e2Stakemura {
36901b846e2Stakemura bus_space_write_1(t->bs_base, bsh, offset * 2, value);
37001b846e2Stakemura }
37101b846e2Stakemura
37201b846e2Stakemura void
com_hpcio_bs_wm_1(bus_space_tag_t t,bus_space_handle_t bsh,bus_size_t offset,const u_int8_t * addr,bus_size_t count)37301b846e2Stakemura com_hpcio_bs_wm_1(bus_space_tag_t t, bus_space_handle_t bsh,
37401b846e2Stakemura bus_size_t offset, const u_int8_t *addr, bus_size_t count)
37501b846e2Stakemura {
37601b846e2Stakemura bus_space_write_multi_1(t->bs_base, bsh, offset * 2, addr, count);
37701b846e2Stakemura }
378