1 1.5 msaitoh /* $NetBSD: lance.c,v 1.5 2021/12/05 03:04:41 msaitoh Exp $ */ 2 1.1 tsutsui 3 1.1 tsutsui /* 4 1.1 tsutsui * Copyright (c) 2013 Izumi Tsutsui. All rights reserved. 5 1.1 tsutsui * 6 1.1 tsutsui * Redistribution and use in source and binary forms, with or without 7 1.1 tsutsui * modification, are permitted provided that the following conditions 8 1.1 tsutsui * are met: 9 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright 10 1.1 tsutsui * notice, this list of conditions and the following disclaimer. 11 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the 13 1.1 tsutsui * documentation and/or other materials provided with the distribution. 14 1.1 tsutsui * 15 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 1.1 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.1 tsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.1 tsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.1 tsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 1.1 tsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 1.1 tsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 1.1 tsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 1.1 tsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 1.1 tsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 1.1 tsutsui */ 26 1.1 tsutsui /*- 27 1.1 tsutsui * Copyright (c) 2004 The NetBSD Foundation, Inc. 28 1.1 tsutsui * All rights reserved. 29 1.1 tsutsui * 30 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation 31 1.1 tsutsui * by UCHIYAMA Yasushi. 32 1.1 tsutsui * 33 1.1 tsutsui * Redistribution and use in source and binary forms, with or without 34 1.1 tsutsui * modification, are permitted provided that the following conditions 35 1.1 tsutsui * are met: 36 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright 37 1.1 tsutsui * notice, this list of conditions and the following disclaimer. 38 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright 39 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the 40 1.1 tsutsui * documentation and/or other materials provided with the distribution. 41 1.1 tsutsui * 42 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 43 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 44 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 45 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 46 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 47 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 48 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 49 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 50 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 51 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 52 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE. 53 1.1 tsutsui */ 54 1.1 tsutsui 55 1.1 tsutsui /* 56 1.1 tsutsui * LANCE driver for LUNA 57 1.1 tsutsui * based on sys/arch/ews4800mips/stand/common/lance.c 58 1.1 tsutsui */ 59 1.1 tsutsui 60 1.1 tsutsui #include <lib/libsa/stand.h> 61 1.1 tsutsui #include <lib/libkern/libkern.h> 62 1.1 tsutsui 63 1.1 tsutsui #include <dev/ic/am7990reg.h> 64 1.1 tsutsui #include <dev/ic/lancereg.h> 65 1.1 tsutsui 66 1.1 tsutsui #include <luna68k/stand/boot/samachdep.h> 67 1.1 tsutsui #include <luna68k/stand/boot/lance.h> 68 1.1 tsutsui 69 1.1 tsutsui static void lance_setup(struct le_softc *); 70 1.1 tsutsui static bool lance_set_initblock(struct le_softc *); 71 1.1 tsutsui static bool lance_do_initialize(struct le_softc *); 72 1.1 tsutsui 73 1.1 tsutsui #define NLE 1 /* XXX for now */ 74 1.1 tsutsui static struct le_softc lesc[NLE]; 75 1.1 tsutsui 76 1.1 tsutsui void * 77 1.1 tsutsui lance_attach(int unit, void *reg, void *mem, uint8_t *eaddr) 78 1.1 tsutsui { 79 1.1 tsutsui struct le_softc *sc; 80 1.1 tsutsui 81 1.1 tsutsui if (unit >= NLE) { 82 1.1 tsutsui printf("%s: invalid unit number\n", __func__); 83 1.1 tsutsui return NULL; 84 1.1 tsutsui } 85 1.1 tsutsui sc = &lesc[unit]; 86 1.1 tsutsui 87 1.1 tsutsui if (sc->sc_reg != NULL) { 88 1.1 tsutsui printf("%s: unit %d is already attached\n", __func__, unit); 89 1.1 tsutsui return NULL; 90 1.1 tsutsui } 91 1.1 tsutsui sc->sc_reg = reg; 92 1.1 tsutsui sc->sc_mem = mem; 93 1.1 tsutsui memcpy(sc->sc_enaddr, eaddr, 6); 94 1.1 tsutsui 95 1.1 tsutsui return sc; 96 1.1 tsutsui } 97 1.1 tsutsui 98 1.1 tsutsui void * 99 1.1 tsutsui lance_cookie(int unit) 100 1.1 tsutsui { 101 1.1 tsutsui struct le_softc *sc; 102 1.1 tsutsui 103 1.1 tsutsui if (unit >= NLE) 104 1.1 tsutsui return NULL; 105 1.1 tsutsui 106 1.1 tsutsui sc = &lesc[unit]; 107 1.1 tsutsui 108 1.1 tsutsui if (sc->sc_reg == NULL) 109 1.1 tsutsui return NULL; 110 1.1 tsutsui 111 1.1 tsutsui return sc; 112 1.1 tsutsui } 113 1.1 tsutsui 114 1.1 tsutsui uint8_t * 115 1.1 tsutsui lance_eaddr(void *cookie) 116 1.1 tsutsui { 117 1.1 tsutsui struct le_softc *sc = cookie; 118 1.1 tsutsui 119 1.1 tsutsui if (sc == NULL || sc->sc_reg == NULL) 120 1.1 tsutsui return NULL; 121 1.1 tsutsui 122 1.1 tsutsui return sc->sc_enaddr; 123 1.1 tsutsui } 124 1.1 tsutsui 125 1.1 tsutsui bool 126 1.1 tsutsui lance_init(void *cookie) 127 1.1 tsutsui { 128 1.1 tsutsui struct le_softc *sc = cookie; 129 1.1 tsutsui 130 1.1 tsutsui lance_setup(sc); 131 1.1 tsutsui 132 1.1 tsutsui if (!lance_set_initblock(sc)) 133 1.1 tsutsui return false; 134 1.1 tsutsui 135 1.1 tsutsui if (!lance_do_initialize(sc)) 136 1.1 tsutsui return false; 137 1.1 tsutsui 138 1.1 tsutsui return true; 139 1.1 tsutsui } 140 1.1 tsutsui 141 1.1 tsutsui int 142 1.1 tsutsui lance_get(void *cookie, void *data, size_t maxlen) 143 1.1 tsutsui { 144 1.1 tsutsui struct le_softc *sc = cookie; 145 1.1 tsutsui struct lereg *lereg = sc->sc_reg; 146 1.1 tsutsui struct lemem *lemem = sc->sc_mem; 147 1.1 tsutsui struct lermd_v *rmd; 148 1.3 tsutsui uint16_t csr __unused; 149 1.1 tsutsui int len = -1; 150 1.1 tsutsui 151 1.1 tsutsui lereg->ler_rap = LE_CSR0; 152 1.1 tsutsui if ((lereg->ler_rdp & LE_C0_RINT) != 0) 153 1.1 tsutsui lereg->ler_rdp = LE_C0_RINT; 154 1.1 tsutsui rmd = &lemem->lem_rmd[sc->sc_currmd]; 155 1.1 tsutsui if ((rmd->rmd1_bits & LE_R1_OWN) != 0) 156 1.1 tsutsui return -1; 157 1.2 tsutsui 158 1.1 tsutsui csr = lereg->ler_rdp; 159 1.1 tsutsui #if 0 160 1.1 tsutsui if ((csr & LE_C0_ERR) != 0) 161 1.1 tsutsui printf("%s: RX poll error (CSR=0x%x)\n", __func__, csr); 162 1.1 tsutsui #endif 163 1.1 tsutsui if ((rmd->rmd1_bits & LE_R1_ERR) != 0) { 164 1.1 tsutsui printf("%s: RX error (rmd status=0x%x)\n", __func__, 165 1.1 tsutsui rmd->rmd1_bits); 166 1.1 tsutsui goto out; 167 1.1 tsutsui } 168 1.1 tsutsui 169 1.1 tsutsui len = rmd->rmd3; 170 1.1 tsutsui if (len < LEMINSIZE + 4 || len > LEMTU) { 171 1.1 tsutsui printf("%s: RX error (bad length %d)\n", __func__, len); 172 1.1 tsutsui goto out; 173 1.1 tsutsui } 174 1.1 tsutsui len -= 4; 175 1.4 riastrad memcpy(data, (void *)lemem->lem_rbuf[sc->sc_currmd], uimin(len, maxlen)); 176 1.1 tsutsui 177 1.1 tsutsui out: 178 1.1 tsutsui rmd->rmd2 = -LEMTU; 179 1.1 tsutsui rmd->rmd1_bits = LE_R1_OWN; /* return to LANCE */ 180 1.1 tsutsui sc->sc_currmd = LE_NEXTRMD(sc->sc_currmd); 181 1.1 tsutsui 182 1.1 tsutsui return len; 183 1.1 tsutsui } 184 1.1 tsutsui 185 1.1 tsutsui bool 186 1.1 tsutsui lance_put(void *cookie, void *data, size_t len) 187 1.1 tsutsui { 188 1.1 tsutsui struct le_softc *sc = cookie; 189 1.1 tsutsui struct lereg *lereg = sc->sc_reg; 190 1.1 tsutsui struct lemem *lemem = sc->sc_mem; 191 1.1 tsutsui struct letmd_v *tmd; 192 1.1 tsutsui uint16_t stat; 193 1.1 tsutsui int timeout; 194 1.1 tsutsui 195 1.1 tsutsui lereg->ler_rap = LE_CSR0; 196 1.1 tsutsui stat = lereg->ler_rdp; 197 1.1 tsutsui lereg->ler_rdp = 198 1.1 tsutsui stat & (LE_C0_BABL | LE_C0_CERR | LE_C0_MISS | LE_C0_TINT); 199 1.1 tsutsui #if 0 200 1.1 tsutsui if (stat & (LE_C0_BABL | LE_C0_CERR | LE_C0_MISS | LE_C0_MERR)) 201 1.1 tsutsui printf("%s: TX error before xmit csr0=0x%x\n", 202 1.1 tsutsui __func__, stat); 203 1.1 tsutsui #endif 204 1.1 tsutsui 205 1.1 tsutsui /* setup TX descriptor */ 206 1.1 tsutsui tmd = &lemem->lem_tmd[sc->sc_curtmd]; 207 1.1 tsutsui while (tmd->tmd1_bits & LE_T1_OWN) 208 1.1 tsutsui continue; 209 1.1 tsutsui tmd->tmd1_bits = LE_T1_STP | LE_T1_ENP; 210 1.1 tsutsui memcpy((void *)lemem->lem_tbuf[sc->sc_curtmd], data, len); 211 1.4 riastrad tmd->tmd2 = -uimax(len, LEMINSIZE); 212 1.1 tsutsui tmd->tmd3 = 0; 213 1.1 tsutsui 214 1.1 tsutsui /* start TX */ 215 1.1 tsutsui tmd->tmd1_bits |= LE_T1_OWN; 216 1.1 tsutsui lereg->ler_rap = LE_CSR0; 217 1.1 tsutsui lereg->ler_rdp = LE_C0_TDMD; 218 1.1 tsutsui 219 1.1 tsutsui /* check TX complete */ 220 1.1 tsutsui timeout = 0; 221 1.1 tsutsui do { 222 1.1 tsutsui lereg->ler_rap = LE_CSR0; 223 1.1 tsutsui stat = lereg->ler_rdp; 224 1.1 tsutsui #if 0 225 1.1 tsutsui if (stat & LE_C0_ERR) { 226 1.1 tsutsui printf("%s: TX error (CSR0=%x)\n", __func__, stat); 227 1.1 tsutsui if (stat & LE_C0_CERR) { 228 1.1 tsutsui lereg->ler_rdp = LE_C0_CERR; 229 1.1 tsutsui } 230 1.1 tsutsui } 231 1.1 tsutsui #endif 232 1.1 tsutsui if (timeout++ > 1000) { 233 1.1 tsutsui printf("%s: TX timeout (CSR0=%x)\n", __func__, stat); 234 1.1 tsutsui return false; 235 1.1 tsutsui } 236 1.1 tsutsui } while ((stat & LE_C0_TINT) == 0); 237 1.1 tsutsui 238 1.1 tsutsui lereg->ler_rdp = LE_C0_TINT; 239 1.1 tsutsui 240 1.1 tsutsui sc->sc_curtmd = LE_NEXTTMD(sc->sc_curtmd); 241 1.1 tsutsui 242 1.1 tsutsui return true; 243 1.1 tsutsui } 244 1.1 tsutsui 245 1.1 tsutsui bool 246 1.1 tsutsui lance_end(void *cookie) 247 1.1 tsutsui { 248 1.1 tsutsui struct le_softc *sc = cookie; 249 1.1 tsutsui struct lereg *lereg = sc->sc_reg; 250 1.1 tsutsui 251 1.1 tsutsui lereg->ler_rap = LE_CSR0; 252 1.1 tsutsui lereg->ler_rdp = LE_C0_STOP; 253 1.1 tsutsui 254 1.1 tsutsui return true; 255 1.1 tsutsui } 256 1.1 tsutsui 257 1.1 tsutsui /* XXX */ 258 1.1 tsutsui int 259 1.1 tsutsui lance_intr(void) 260 1.1 tsutsui { 261 1.1 tsutsui 262 1.1 tsutsui return 1; 263 1.1 tsutsui } 264 1.1 tsutsui 265 1.1 tsutsui static bool 266 1.1 tsutsui lance_set_initblock(struct le_softc *sc) 267 1.1 tsutsui { 268 1.1 tsutsui struct lereg *lereg = sc->sc_reg; 269 1.1 tsutsui uint32_t addr = (uint32_t)sc->sc_mem; 270 1.1 tsutsui 271 1.1 tsutsui lereg->ler_rap = LE_CSR0; 272 1.1 tsutsui lereg->ler_rdp = LE_C0_STOP; /* disable all external activity */ 273 1.1 tsutsui DELAY(100); 274 1.1 tsutsui 275 1.1 tsutsui /* Set the correct byte swapping mode */ 276 1.1 tsutsui lereg->ler_rap = LE_CSR3; 277 1.1 tsutsui lereg->ler_rdp = LE_C3_BSWP; 278 1.1 tsutsui 279 1.1 tsutsui /* Low address of init block */ 280 1.1 tsutsui lereg->ler_rap = LE_CSR1; 281 1.1 tsutsui lereg->ler_rdp = addr & 0xfffe; 282 1.1 tsutsui 283 1.1 tsutsui /* High address of init block */ 284 1.1 tsutsui lereg->ler_rap = LE_CSR2; 285 1.1 tsutsui lereg->ler_rdp = (addr >> 16) & 0x00ff; 286 1.1 tsutsui DELAY(100); 287 1.1 tsutsui 288 1.1 tsutsui return true; 289 1.1 tsutsui } 290 1.1 tsutsui 291 1.1 tsutsui static bool 292 1.1 tsutsui lance_do_initialize(struct le_softc *sc) 293 1.1 tsutsui { 294 1.1 tsutsui struct lereg *lereg = sc->sc_reg; 295 1.1 tsutsui uint16_t reg; 296 1.1 tsutsui int timeout; 297 1.1 tsutsui 298 1.1 tsutsui sc->sc_curtmd = 0; 299 1.1 tsutsui sc->sc_currmd = 0; 300 1.1 tsutsui 301 1.5 msaitoh /* Initialize LANCE */ 302 1.1 tsutsui lereg->ler_rap = LE_CSR0; 303 1.1 tsutsui lereg->ler_rdp = LE_C0_INIT; 304 1.1 tsutsui 305 1.1 tsutsui /* Wait interrupt */ 306 1.1 tsutsui timeout = 1000000; 307 1.1 tsutsui do { 308 1.1 tsutsui lereg->ler_rap = LE_CSR0; 309 1.1 tsutsui reg = lereg->ler_rdp; 310 1.1 tsutsui if (--timeout == 0) { 311 1.1 tsutsui printf("le: init timeout (CSR=0x%x)\n", reg); 312 1.1 tsutsui return false; 313 1.1 tsutsui } 314 1.1 tsutsui DELAY(1); 315 1.1 tsutsui } while ((reg & LE_C0_IDON) == 0); 316 1.2 tsutsui 317 1.1 tsutsui lereg->ler_rap = LE_CSR0; 318 1.1 tsutsui lereg->ler_rdp = LE_C0_STRT | LE_C0_IDON; 319 1.1 tsutsui 320 1.1 tsutsui return true; 321 1.1 tsutsui } 322 1.1 tsutsui 323 1.1 tsutsui static void 324 1.1 tsutsui lance_setup(struct le_softc *sc) 325 1.1 tsutsui { 326 1.1 tsutsui struct lereg *lereg = sc->sc_reg; 327 1.1 tsutsui struct lemem *lemem = sc->sc_mem; 328 1.1 tsutsui uint32_t addr; 329 1.1 tsutsui int i; 330 1.1 tsutsui 331 1.1 tsutsui /* make sure to stop LANCE chip before setup memory */ 332 1.1 tsutsui lereg->ler_rap = LE_CSR0; 333 1.1 tsutsui lereg->ler_rdp = LE_C0_STOP; 334 1.1 tsutsui 335 1.1 tsutsui memset(lemem, 0, sizeof *lemem); 336 1.1 tsutsui 337 1.1 tsutsui /* Init block */ 338 1.1 tsutsui lemem->lem_mode = LE_MODE_NORMAL; 339 1.1 tsutsui lemem->lem_padr[0] = (sc->sc_enaddr[1] << 8) | sc->sc_enaddr[0]; 340 1.1 tsutsui lemem->lem_padr[1] = (sc->sc_enaddr[3] << 8) | sc->sc_enaddr[2]; 341 1.1 tsutsui lemem->lem_padr[2] = (sc->sc_enaddr[5] << 8) | sc->sc_enaddr[4]; 342 1.1 tsutsui /* Logical address filter */ 343 1.1 tsutsui for (i = 0; i < 4; i++) 344 1.1 tsutsui lemem->lem_ladrf[i] = 0x0000; 345 1.1 tsutsui 346 1.1 tsutsui /* Location of Rx descriptor ring */ 347 1.1 tsutsui addr = (uint32_t)lemem->lem_rmd; 348 1.1 tsutsui lemem->lem_rdra = addr & 0xffff; 349 1.1 tsutsui lemem->lem_rlen = LE_RLEN | ((addr >> 16) & 0xff); 350 1.1 tsutsui 351 1.1 tsutsui /* Location of Tx descriptor ring */ 352 1.1 tsutsui addr = (uint32_t)lemem->lem_tmd; 353 1.1 tsutsui lemem->lem_tdra = addr & 0xffff; 354 1.1 tsutsui lemem->lem_tlen = LE_TLEN | ((addr >> 16) & 0xff); 355 1.1 tsutsui 356 1.1 tsutsui /* Rx descriptor */ 357 1.1 tsutsui for (i = 0; i < LERBUF; i++) { 358 1.1 tsutsui addr = (uint32_t)lemem->lem_rbuf[i]; 359 1.1 tsutsui lemem->lem_rmd[i].rmd0 = addr & 0xffff; 360 1.1 tsutsui lemem->lem_rmd[i].rmd1_hadr = (addr >> 16) & 0xff; 361 1.1 tsutsui lemem->lem_rmd[i].rmd1_bits = LE_R1_OWN; 362 1.1 tsutsui lemem->lem_rmd[i].rmd2 = LE_XMD2_ONES | -LEMTU; 363 1.1 tsutsui lemem->lem_rmd[i].rmd3 = 0; 364 1.1 tsutsui } 365 1.1 tsutsui 366 1.1 tsutsui /* Tx descriptor */ 367 1.1 tsutsui for (i = 0; i < LETBUF; i++) { 368 1.1 tsutsui addr = (uint32_t)lemem->lem_tbuf[i]; 369 1.1 tsutsui lemem->lem_tmd[i].tmd0 = addr & 0xffff; 370 1.1 tsutsui lemem->lem_tmd[i].tmd1_hadr = (addr >> 16) & 0xff; 371 1.1 tsutsui lemem->lem_tmd[i].tmd1_bits = 0; 372 1.1 tsutsui lemem->lem_tmd[i].tmd2 = LE_XMD2_ONES | 0; 373 1.1 tsutsui lemem->lem_tmd[i].tmd3 = 0; 374 1.1 tsutsui } 375 1.1 tsutsui } 376