1 /* $NetBSD: wdc_extio.c,v 1.13 2022/09/29 07:00:46 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 David Young. All rights reserved. 5 * 6 * CompactFlash attachment for RouterBoard 153. Derived from 7 * the NetBSD/mac68k OBIO-IDE attachment. 8 * 9 * Redistribution and use in source and binary forms, with or 10 * without modification, are permitted provided that the following 11 * conditions are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer in the documentation and/or other materials provided 17 * with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 26 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 * OF SUCH DAMAGE. 31 */ 32 /* 33 * Copyright (c) 2002 Takeshi Shibagaki All rights reserved. 34 * 35 * mac68k OBIO-IDE attachment created by Takeshi Shibagaki 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by Charles M. Hannum. 48 * 4. The name of the author may not be used to endorse or promote products 49 * derived from this software without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: wdc_extio.c,v 1.13 2022/09/29 07:00:46 skrll Exp $"); 65 66 #include <sys/param.h> 67 #include <sys/bus.h> 68 #include <sys/callout.h> 69 #include <sys/cpu.h> 70 #include <sys/device.h> 71 #include <sys/intr.h> 72 #include <sys/kernel.h> 73 #include <sys/systm.h> 74 75 #include <mips/adm5120/include/adm5120_extiovar.h> 76 77 #include <dev/ata/atareg.h> 78 #include <dev/ata/atavar.h> 79 #include <dev/ic/wdcvar.h> 80 81 #ifdef WDC_EXTIO_DEBUG 82 int wdc_extio_debug = 1; 83 #define WDC_EXTIO_DPRINTF(__fmt, ...) \ 84 do { \ 85 if (wdc_extio_debug) \ 86 printf((__fmt), __VA_ARGS__); \ 87 } while (/*CONSTCOND*/0) 88 #else /* !WDC_EXTIO_DEBUG */ 89 #define WDC_EXTIO_DPRINTF(__fmt, ...) do { } while (/*CONSTCOND*/0) 90 #endif /* WDC_EXTIO_DEBUG */ 91 92 #define WDC_OBIO_CF_WINSIZE 0x1000 93 #define WDC_OBIO_REG_OFFSET 0x0800 94 #define WDC_OBIO_DATA_OFFSET 0x0808 95 #define WDC_OBIO_AUXREG_OFFSET 0x080e 96 97 struct wdc_extio_softc { 98 struct wdc_softc sc_wdcdev; 99 struct ata_channel *sc_chanlist[1]; 100 struct ata_channel sc_channel; 101 struct wdc_regs sc_wdc_regs; 102 void *sc_gpio; 103 int sc_map; 104 struct gpio_pinmap sc_pinmap; 105 }; 106 107 int wdc_extio_match(device_t, cfdata_t, void *); 108 void wdc_extio_attach(device_t, device_t, void *); 109 110 CFATTACH_DECL_NEW(wdc_extio, sizeof(struct wdc_extio_softc), 111 wdc_extio_match, wdc_extio_attach, NULL, NULL); 112 113 #if 0 114 void wdc_extio_reset(struct ata_channel *, int); 115 void 116 wdc_extio_reset(struct ata_channel *chp, int poll) 117 { 118 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 119 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 120 uint8_t st0; 121 int i, s = 0; 122 123 if (poll) 124 s = splbio(); 125 if (wdc->select) 126 wdc->select(chp,0); 127 /* assert SRST, wait for reset to complete */ 128 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_RST); 129 delay(2000); 130 (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0); 131 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 0); 132 delay(10); /* 400ns delay */ 133 for (i = 3100000; --i > 0; ) { 134 st0 = bus_space_read_1(wdr->cmd_iot, 135 wdr->cmd_iohs[wd_status], 0); 136 if ((st0 & WDCS_BSY) == 0) 137 break; 138 delay(10); 139 } 140 if (i == 0) 141 printf("%s: reset failed\n", __func__); 142 else 143 WDC_EXTIO_DPRINTF("%s: reset in %d.%02dms\n", __func__, 144 (3100000 - i) / 100, 145 (3100000 - i) % 100); 146 147 if (poll) { 148 /* ACK interrupt in case there is one pending left */ 149 if (wdc->irqack) 150 wdc->irqack(chp); 151 splx(s); 152 } 153 } 154 #endif 155 156 static int 157 wdc_extio_map(struct extio_attach_args *ea, struct wdc_regs *wdr, 158 void **gpio, struct gpio_pinmap *pinmap) 159 { 160 int i; 161 162 *gpio = ea->ea_gpio; 163 wdr->cmd_iot = wdr->ctl_iot = ea->ea_st; 164 165 #if 0 166 if (gpio_pin_map(ea->ea_gpio, 0, ea->ea_gpio_mask, pinmap) != 0) { 167 printf("%s: couldn't map GPIO\n", __func__); 168 return -1; 169 } 170 #endif 171 172 if (bus_space_map(wdr->cmd_iot, ea->ea_addr, WDC_OBIO_CF_WINSIZE, 0, 173 &wdr->cmd_baseioh)) { 174 aprint_error("%s: couldn't map registers\n", __func__); 175 goto post_gpio_err; 176 } 177 178 for (i = 1; i < WDC_NREG; i++) { 179 if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, 180 WDC_OBIO_REG_OFFSET + i, 1, 181 &wdr->cmd_iohs[i]) != 0) { 182 aprint_error( 183 "%s: unable to subregion control register\n", 184 __func__); 185 goto post_bus_err; 186 } 187 } 188 if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, 189 WDC_OBIO_DATA_OFFSET, 2, 190 &wdr->cmd_iohs[wd_data]) != 0) { 191 aprint_error("%s: unable to subregion data register\n", 192 __func__); 193 goto post_bus_err; 194 } 195 if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, 196 WDC_OBIO_AUXREG_OFFSET, 1, &wdr->ctl_ioh)) { 197 aprint_error("%s: unable to subregion aux register\n", 198 __func__); 199 goto post_bus_err; 200 } 201 202 wdc_init_shadow_regs(wdr); 203 204 return 0; 205 post_bus_err: 206 bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, WDC_OBIO_CF_WINSIZE); 207 post_gpio_err: 208 #if 0 209 gpio_pin_unmap(ea->ea_gpio, pinmap); 210 #endif 211 return -1; 212 } 213 214 static void 215 wdc_extio_dataout(struct ata_channel *chp, int flags, void *bf, size_t len) 216 { 217 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 218 219 bus_space_write_multi_1( 220 wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len); 221 } 222 223 static void 224 wdc_extio_datain(struct ata_channel *chp, int flags, void *bf, size_t len) 225 { 226 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 227 228 bus_space_read_multi_1( 229 wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len); 230 } 231 232 int 233 wdc_extio_match(device_t parent, cfdata_t cf, void *aux) 234 { 235 struct extio_attach_args *ea = (struct extio_attach_args *)aux; 236 struct wdc_regs wdr; 237 int result = 0; 238 void *gpio; 239 int map; 240 struct gpio_pinmap pm = {.pm_map = &map}; 241 242 if (strcmp(ea->ea_name, cf->cf_name) != 0) 243 return 0; 244 245 WDC_EXTIO_DPRINTF("%s: enter\n", __func__); 246 247 #if 0 248 wdc.datain_pio = wdc_extio_datain; 249 wdc.dataout_pio = wdc_extio_dataout; 250 if (cf->cf_flags != -1) { 251 wdc.sc_atac.atac_cap |= cf->cf_flags & 252 (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16); 253 wdc.cap |= cf->cf_flags & 254 (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS); 255 } 256 #endif 257 258 if (wdc_extio_map(ea, &wdr, &gpio, &pm) == -1) 259 return 0; 260 261 result = wdcprobe(&wdr); 262 263 bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_OBIO_CF_WINSIZE); 264 #if 0 265 gpio_pin_unmap(ea->ea_gpio, &pm); 266 #endif 267 268 return result; 269 } 270 271 void 272 wdc_extio_attach(device_t parent, device_t self, void *aux) 273 { 274 struct wdc_extio_softc *sc = device_private(self); 275 struct wdc_regs *wdr; 276 struct extio_attach_args *ea = aux; 277 struct ata_channel *chp = &sc->sc_channel; 278 struct cfdata *cf; 279 280 WDC_EXTIO_DPRINTF("%s: enter\n", __func__); 281 282 sc->sc_wdcdev.sc_atac.atac_dev = self; 283 sc->sc_pinmap.pm_map = &sc->sc_map; 284 sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs; 285 chp->ch_atac = &sc->sc_wdcdev.sc_atac; 286 287 if (wdc_extio_map(ea, wdr, &sc->sc_gpio, &sc->sc_pinmap) == -1) 288 return; 289 290 cf = device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev); 291 if (cf->cf_flags != -1) { 292 aprint_normal(" flags %04x", cf->cf_flags); 293 sc->sc_wdcdev.sc_atac.atac_cap |= cf->cf_flags & 294 (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16); 295 sc->sc_wdcdev.cap |= cf->cf_flags & 296 (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS); 297 } 298 sc->sc_wdcdev.datain_pio = wdc_extio_datain; 299 sc->sc_wdcdev.dataout_pio = wdc_extio_dataout; 300 sc->sc_wdcdev.sc_atac.atac_pio_cap = 0; 301 sc->sc_chanlist[0] = chp; 302 sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist; 303 sc->sc_wdcdev.sc_atac.atac_nchannels = 1; 304 sc->sc_wdcdev.wdc_maxdrives = 2; 305 306 chp->ch_channel = 0; 307 chp->ch_atac = &sc->sc_wdcdev.sc_atac; 308 309 aprint_normal("\n"); 310 311 wdcattach(chp); 312 } 313