1 /* $NetBSD: pq3duart.c,v 1.3 2011/08/01 17:05:17 matt Exp $ */ 2 /*- 3 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects 8 * Agency and which was developed by Matt Thomas of 3am Software Foundry. 9 * 10 * This material is based upon work supported by the Defense Advanced Research 11 * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under 12 * Contract No. N66001-09-C-2073. 13 * Approved for Public Release, Distribution Unlimited 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 39 __KERNEL_RCSID(0, "$NetBSD: pq3duart.c,v 1.3 2011/08/01 17:05:17 matt Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/cpu.h> 43 #include <sys/device.h> 44 #include <sys/tty.h> 45 46 #include "ioconf.h" 47 48 #include <sys/intr.h> 49 #include <sys/bus.h> 50 51 #include <dev/ic/comreg.h> 52 #include <dev/ic/comvar.h> 53 54 #include <powerpc/booke/cpuvar.h> 55 #include <powerpc/booke/e500var.h> 56 #include <powerpc/booke/e500reg.h> 57 58 struct pq3duart_softc { 59 device_t dsc_dev; 60 struct com_softc *dsc_sc[2]; 61 bus_space_tag_t dsc_memt; 62 bus_addr_t dsc_base; 63 void *dsc_ih; 64 }; 65 66 struct pq3duart_attach_args { 67 const char *da_busname; 68 bus_space_tag_t da_memt; 69 bus_addr_t da_addr; 70 bus_addr_t da_size; 71 u_int da_port; 72 }; 73 74 static int pq3duart_match(device_t, cfdata_t, void *); 75 static void pq3duart_attach(device_t, device_t, void *); 76 static int com_pq3duart_match(device_t, cfdata_t, void *); 77 static void com_pq3duart_attach(device_t, device_t, void *); 78 79 CFATTACH_DECL_NEW(pq3duart, sizeof(struct pq3duart_softc), 80 pq3duart_match, pq3duart_attach, NULL, NULL); 81 82 CFATTACH_DECL_NEW(com_pq3duart, sizeof(struct com_softc), 83 com_pq3duart_match, com_pq3duart_attach, NULL, NULL); 84 85 static int 86 pq3duart_match(device_t parent, cfdata_t cf, void *aux) 87 { 88 89 if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux)) 90 return 0; 91 92 return 1; 93 } 94 95 static int 96 com_pq3duart_match(device_t parent, cfdata_t cfdata, void *aux) 97 { 98 struct pq3duart_softc * const dsc = device_private(parent); 99 struct pq3duart_attach_args * const da = aux; 100 struct com_regs regs; 101 102 if ((da->da_port != 1 && da->da_port != 2) 103 || dsc->dsc_sc[da->da_port-1] != NULL) 104 return 0; 105 106 bus_space_tag_t memt = da->da_memt; 107 bus_addr_t addr = da->da_addr; 108 bus_addr_t size = da->da_size; 109 bus_space_handle_t memh; 110 111 if (com_is_console(memt, addr, &memh)) 112 return 1; 113 114 if (bus_space_map(memt, addr, size, 0, &memh)) 115 return 0; 116 117 COM_INIT_REGS(regs, memt, memh, addr); 118 119 int rv = com_probe_subr(®s); 120 121 bus_space_unmap(memt, memh, size); 122 123 return rv; 124 } 125 126 static int 127 pq3duart_intr(void *arg) 128 { 129 struct pq3duart_softc * const dsc = arg; 130 int rv = 0; 131 132 if (dsc->dsc_sc[0] != NULL) 133 rv += comintr(dsc->dsc_sc[0]); 134 if (dsc->dsc_sc[1] != NULL) 135 rv += comintr(dsc->dsc_sc[1]); 136 137 return rv; 138 } 139 140 static int 141 pq3duart_print(void *aux, const char *pnp) 142 { 143 struct pq3duart_attach_args * const da = aux; 144 145 if (pnp != NULL) 146 return QUIET; 147 148 aprint_normal(" port %d", da->da_port); 149 150 return UNCONF; 151 } 152 153 static void 154 pq3duart_attach(device_t parent, device_t self, void *aux) 155 { 156 struct cpunode_softc * const psc = device_private(parent); 157 struct pq3duart_softc * const dsc = device_private(self); 158 struct cpunode_attach_args * const cna = aux; 159 struct cpunode_locators * const cnl = &cna->cna_locs; 160 struct pq3duart_attach_args da; 161 u_int nports = cnl->cnl_size / DUART_SIZE; 162 163 psc->sc_children |= cna->cna_childmask; 164 dsc->dsc_dev = self; 165 166 aprint_normal(": %u ports\n", nports); 167 168 for (u_int port = 1; port <= min(2, nports); port++) { 169 da.da_memt = cna->cna_memt; 170 da.da_port = port; 171 da.da_addr = cnl->cnl_addr + (port - 1) * DUART_SIZE; 172 da.da_size = COM_NPORTS; 173 (void)config_found_sm_loc(self, "duart", NULL, 174 &da, pq3duart_print, NULL); 175 } 176 177 if (dsc->dsc_sc[0] != NULL || dsc->dsc_sc[1] != NULL) { 178 dsc->dsc_ih = intr_establish(cnl->cnl_intrs[0], IPL_SERIAL, 179 IST_ONCHIP, pq3duart_intr, dsc); 180 if (dsc->dsc_ih == NULL) 181 aprint_error_dev(self, 182 "failed to establish interrupt %d\n", 183 cnl->cnl_intrs[0]); 184 else 185 aprint_normal_dev(self, 186 "interrupting on irq %d\n", 187 cnl->cnl_intrs[0]); 188 } 189 } 190 191 static void 192 com_pq3duart_attach(device_t parent, device_t self, void *aux) 193 { 194 struct pq3duart_softc * const dsc = device_private(parent); 195 struct com_softc * const sc = device_private(self); 196 struct pq3duart_attach_args * const da = aux; 197 198 dsc->dsc_sc[da->da_port-1] = sc; 199 sc->sc_dev = self; 200 sc->sc_frequency = (int) board_info_get_number("bus-frequency"); 201 202 bus_space_tag_t memt = da->da_memt; 203 bus_addr_t addr = da->da_addr; 204 bus_addr_t size = da->da_size; 205 bus_space_handle_t memh; 206 207 if (!com_is_console(memt, addr, &memh)) { 208 int error = bus_space_map(memt, addr, size, 0, &memh); 209 if (error) { 210 aprint_error(": failed to map registers: %d\n", error); 211 return; 212 } 213 } 214 215 COM_INIT_REGS(sc->sc_regs, memt, memh, addr); 216 sc->sc_regs.cr_nports = size; 217 218 com_attach_subr(sc); 219 220 } 221