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