2 * Copyright (c) 2004 Hidetoshi Shimokawa <simokawa@FreeBSD.ORG>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * FireWire disk device handling.
37 #include <machine/bootinfo.h>
41 #include <bootstrap.h>
45 #include <dev/dcons/dcons.h>
48 #define BIT4x2(x,y) uint8_t y:4, x:4
49 #define BIT16x2(x,y) uint32_t y:16, x:16
51 #include <dev/firewire/iec13213.h>
53 extern uint32_t dcons_paddr;
54 extern struct console dconsole;
58 struct crom_chunk root;
59 struct crom_chunk vendor;
62 struct crom_chunk unit;
63 struct crom_chunk spec;
64 struct crom_chunk ver;
67 static int fw_init(void);
68 static int fw_strategy(void *devdata, int flag, daddr_t dblk,
69 size_t size, char *buf, size_t *rsize);
70 static int fw_open(struct open_file *f, ...);
71 static int fw_close(struct open_file *f);
72 static void fw_print(int verbose);
73 static void fw_cleanup(void);
77 struct devsw fwohci = {
78 "FW1394", /* 7 chars at most */
89 static struct fwohci_softc fwinfo[MAX_OHCI];
90 static int fw_initialized = 0;
93 fw_probe(int index, struct fwohci_softc *sc)
97 sc->state = FWOHCI_STATE_INIT;
98 err = biospci_find_devclass(
99 0x0c0010 /* Serial:FireWire:OHCI */,
104 sc->state = FWOHCI_STATE_DEAD;
108 biospci_write_config(sc->locator,
110 0x6 /* enable bus master and memory mapped I/O */,
113 biospci_read_config(sc->locator, 0x00 /*devid*/, 2 /*dword*/,
115 biospci_read_config(sc->locator, 0x10 /*base_addr*/, 2 /*dword*/,
118 sc->handle = (uint32_t)PTOV(sc->base_addr);
119 sc->bus_id = OREAD(sc, OHCI_BUS_ID);
128 struct fwohci_softc *sc;
134 for (i = 0; i < MAX_OHCI; i ++) {
137 if (sc->state == FWOHCI_STATE_DEAD)
149 * Print information about OHCI chips
152 fw_print(int verbose)
155 struct fwohci_softc *sc;
157 for (i = 0; i < MAX_OHCI; i ++) {
159 if (sc->state == FWOHCI_STATE_DEAD)
161 printf("%d: locator=0x%04x devid=0x%08x"
162 " base_addr=0x%08x handle=0x%08x bus_id=0x%08x\n",
163 i, sc->locator, sc->devid,
164 sc->base_addr, sc->handle, sc->bus_id);
169 fw_open(struct open_file *f, ...)
173 struct i386_devdesc *dev;
174 struct open_disk *od;
178 dev = va_arg(ap, struct i386_devdesc *);
186 fw_close(struct open_file *f)
194 struct dcons_buf *db;
196 /* invalidate dcons buffer */
198 db = (struct dcons_buf *)PTOV(dcons_paddr);
204 fw_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
210 fw_init_crom(struct fwohci_softc *sc)
212 struct crom_src *src;
214 printf("fw_init_crom\n");
215 sc->crom_src_buf = (struct crom_src_buf *)
216 malloc(sizeof(struct crom_src_buf));
217 if (sc->crom_src_buf == NULL)
220 src = &sc->crom_src_buf->src;
221 bzero(src, sizeof(struct crom_src));
223 /* BUS info sample */
224 src->hdr.info_len = 4;
226 src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
228 src->businfo.irmc = 1;
229 src->businfo.cmc = 1;
230 src->businfo.isc = 1;
231 src->businfo.bmc = 1;
232 src->businfo.pmc = 0;
233 src->businfo.cyc_clk_acc = 100;
234 src->businfo.max_rec = sc->maxrec;
235 src->businfo.max_rom = MAXROM_4;
236 src->businfo.generation = 1;
237 src->businfo.link_spd = sc->speed;
239 src->businfo.eui64.hi = sc->eui.hi;
240 src->businfo.eui64.lo = sc->eui.lo;
242 STAILQ_INIT(&src->chunk_list);
245 sc->crom_root = &sc->crom_src_buf->root;
249 fw_reset_crom(struct fwohci_softc *sc)
251 struct crom_src_buf *buf;
252 struct crom_src *src;
253 struct crom_chunk *root;
255 printf("fw_reset\n");
256 if (sc->crom_src_buf == NULL)
259 buf = sc->crom_src_buf;
261 root = sc->crom_root;
263 STAILQ_INIT(&src->chunk_list);
265 bzero(root, sizeof(struct crom_chunk));
266 crom_add_chunk(src, NULL, root, 0);
267 crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
268 /* private company_id */
269 crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
271 crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
273 crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
278 #define ADDR_HI(x) (((x) >> 24) & 0xffffff)
279 #define ADDR_LO(x) ((x) & 0xffffff)
282 dcons_crom(struct fwohci_softc *sc)
284 struct crom_src_buf *buf;
285 struct crom_src *src;
286 struct crom_chunk *root;
288 buf = sc->crom_src_buf;
290 root = sc->crom_root;
292 bzero(&buf->unit, sizeof(struct crom_chunk));
294 crom_add_chunk(src, root, &buf->unit, CROM_UDIR);
295 crom_add_entry(&buf->unit, CSRKEY_SPEC, CSRVAL_VENDOR_PRIVATE);
296 crom_add_simple_text(src, &buf->unit, &buf->spec, "FreeBSD");
297 crom_add_entry(&buf->unit, CSRKEY_VER, DCONS_CSR_VAL_VER);
298 crom_add_simple_text(src, &buf->unit, &buf->ver, "dcons");
299 crom_add_entry(&buf->unit, DCONS_CSR_KEY_HI, ADDR_HI(dcons_paddr));
300 crom_add_entry(&buf->unit, DCONS_CSR_KEY_LO, ADDR_LO(dcons_paddr));
304 fw_crom(struct fwohci_softc *sc)
306 struct crom_src *src;
312 newrom = malloc(CROMSIZE);
313 src = &sc->crom_src_buf->src;
314 crom_load(src, (uint32_t *)newrom, CROMSIZE);
315 if (bcmp(newrom, sc->config_rom, CROMSIZE) != 0) {
316 /* bump generation and reload */
317 src->businfo.generation ++;
318 /* generation must be between 0x2 and 0xF */
319 if (src->businfo.generation < 2)
320 src->businfo.generation ++;
321 crom_load(src, (uint32_t *)newrom, CROMSIZE);
322 bcopy(newrom, (void *)sc->config_rom, CROMSIZE);
328 fw_busreset(struct fwohci_softc *sc)
332 if (sc->state < FWOHCI_STATE_ENABLED) {
333 printf("fwohci not enabled\n");
339 while (sc->state< FWOHCI_STATE_NORMAL) {
343 printf("give up to wait bus initialize\n");
347 printf("poll count = %d\n", count);
354 struct fwohci_softc *sc;
357 if (fw_initialized == 0)
360 for (i = 0; i < MAX_OHCI; i ++) {
362 if (sc->state != FWOHCI_STATE_INIT)
365 sc->config_rom = (uint32_t *)
366 (((uint32_t)sc->config_rom_buf
367 + (CROMSIZE - 1)) & ~(CROMSIZE - 1));
369 printf("configrom: %08p %08p\n",
370 sc->config_rom_buf, sc->config_rom);
372 if (fwohci_init(sc, 0) == 0) {
373 sc->state = FWOHCI_STATE_ENABLED;
376 sc->state = FWOHCI_STATE_DEAD;
383 struct fwohci_softc *sc;
386 if (fw_initialized == 0)
389 for (i = 0; i < MAX_OHCI; i ++) {
391 if (sc->state < FWOHCI_STATE_ENABLED)
397 #if 0 /* for debug */
399 fw_busreset_cmd(int argc, char *argv[])
401 struct fwohci_softc *sc;
404 for (i = 0; i < MAX_OHCI; i ++) {
406 if (sc->state < FWOHCI_STATE_INIT)
414 fw_poll_cmd(int argc, char *argv[])
421 fw_enable_cmd(int argc, char *argv[])
430 dcons_enable(int argc, char *argv[])
434 dconsole.c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
439 dcons_read(int argc, char *argv[])
442 while (dconsole.c_ready()) {
451 dcons_write(int argc, char *argv[])
457 len = strlen(argv[1]);
458 for (i = 0; i < len; i ++)
459 dconsole.c_out(argv[1][i]);
460 dconsole.c_out('\r');
461 dconsole.c_out('\n');
464 COMMAND_SET(firewire, "firewire", "enable firewire", fw_enable_cmd);
465 COMMAND_SET(fwbusreset, "fwbusreset", "firewire busreset", fw_busreset_cmd);
466 COMMAND_SET(fwpoll, "fwpoll", "firewire poll", fw_poll_cmd);
467 COMMAND_SET(dcons, "dcons", "enable dcons", dcons_enable);
468 COMMAND_SET(dread, "dread", "read from dcons", dcons_read);
469 COMMAND_SET(dwrite, "dwrite", "write to dcons", dcons_write);