Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / sandpoint / stand / netboot / atawd.c
blob2fcef6b2b0ebbebb19990b16c1cbc02dc4e18435
1 /* $NetBSD: atawd.c,v 1.8 2009/01/12 08:06:54 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tohru Nishimura.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/disklabel.h>
36 #include <sys/bootblock.h>
37 #include <dev/raidframe/raidframevar.h>
38 #include <dev/ic/wdcreg.h>
39 #include <dev/ata/atareg.h>
41 #include <lib/libsa/stand.h>
42 #include <lib/libkern/libkern.h>
43 #include <machine/stdarg.h>
45 #include "globals.h"
47 #if defined(_DEBUG)
48 #define DPRINTF(x) printf x;
49 #else
50 #define DPRINTF(x)
51 #endif
53 struct atacdv {
54 int (*match)(unsigned, void *);
55 void *(*init)(unsigned, void *);
56 unsigned chvalid;
57 void *priv;
60 #define ATAC_DECL(xxx) \
61 int xxx ## _match(unsigned, void *); \
62 void * xxx ## _init(unsigned, void *)
64 ATAC_DECL(pciide);
66 static struct atacdv vatacdv[] = {
67 { pciide_match, pciide_init, 01 },
69 static int natacdv = sizeof(vatacdv)/sizeof(vatacdv[0]);
70 struct atacdv *atac;
72 void *disk[4];
73 int ndisk;
75 static int wd_get_params(struct wd_softc *);
76 static int wdgetdisklabel(struct wd_softc *);
78 int atac_init(unsigned);
79 int atac_probe(void *);
80 static int atac_wait_for_ready(struct atac_channel *);
81 static int atac_exec_identify(struct wd_softc *, void *);
82 static int atac_read_block(struct wd_softc *, struct atac_command *);
83 static int atacommand(struct wd_softc *, struct atac_command *);
84 static int atac_exec_read(struct wd_softc *, int, daddr_t, void *);
86 #define WDC_TIMEOUT 2000000
88 int
89 wdopen(struct open_file *f, ...)
91 va_list ap;
92 int unit, part;
93 struct wd_softc *wd;
94 struct disklabel *lp;
95 struct partition *pp;
97 va_start(ap, f);
98 unit = va_arg(ap, u_int);
99 part = va_arg(ap, u_int);
100 va_end(ap);
102 if (unit >= ndisk)
103 return ENXIO;
104 wd = disk[unit];
105 lp = &wd->sc_label;
106 if (part >= lp->d_npartitions)
107 return ENXIO;
108 pp = &lp->d_partitions[part];
109 if (pp->p_size == 0 || pp->p_fstype == FS_UNUSED)
110 return ENXIO;
111 wd->sc_part = part;
112 f->f_devdata = wd;
113 return 0;
116 int
117 wdclose(struct open_file *f)
120 f->f_devdata = NULL;
121 return 0;
124 int
125 wdstrategy(void *f, int rw, daddr_t dblk, size_t size, void *p, size_t *rsize)
127 int i, nsect;
128 daddr_t blkno;
129 struct wd_softc *wd;
130 struct partition *pp;
131 uint8_t *buf;
133 if (size == 0)
134 return 0;
136 if (rw != F_READ)
137 return EOPNOTSUPP;
139 buf = p;
140 wd = f;
141 pp = &wd->sc_label.d_partitions[wd->sc_part];
143 nsect = howmany(size, wd->sc_label.d_secsize);
144 blkno = dblk + pp->p_offset;
145 if (pp->p_fstype == FS_RAID)
146 blkno += RF_PROTECTED_SECTORS;
148 for (i = 0; i < nsect; i++, blkno++) {
149 int error;
151 if ((error = atac_exec_read(wd, WDCC_READ, blkno, buf)) != 0)
152 return error;
154 buf += wd->sc_label.d_secsize;
157 *rsize = size;
158 return 0;
162 parsefstype(void *data)
164 struct wd_softc *wd = data;
165 struct disklabel *lp = &wd->sc_label;
166 struct partition *pp = &lp->d_partitions[wd->sc_part];
168 return pp->p_fstype;
172 atac_init(unsigned tag)
174 struct atacdv *dv;
175 int n;
177 for (n = 0; n < natacdv; n++) {
178 dv = &vatacdv[n];
179 if ((*dv->match)(tag, NULL) > 0)
180 goto found;
182 return 0;
183 found:
184 atac = dv;
185 atac->priv = (*dv->init)(tag, (void *)dv->chvalid);
186 return 1;
190 atac_probe(void *atac)
192 struct atac_softc *l = atac;
193 struct wd_softc *wd;
194 int i, error, chvalid;
196 i = 0; error = 0;
197 chvalid = l->chvalid;
198 for (i = 0; chvalid != 0; i += 1) {
199 if (chvalid & (01 << i)) {
200 chvalid &= ~(01 << i);
201 #if 0
202 error = diskprobe(atac);
203 if (error != 0)
204 continue;
205 #endif
206 wd = alloc(sizeof(struct wd_softc));
207 memset(wd, 0, sizeof(struct wd_softc));
208 wd->sc_unit = ndisk;
209 wd->sc_channel = &l->channel[i];
210 disk[ndisk] = (void *)wd;
211 error = wd_get_params(wd);
212 if (error != 0)
213 continue;
214 error = wdgetdisklabel(wd);
215 if (error != 0)
216 continue;
217 ndisk += 1;
220 return error;
223 static int
224 wd_get_params(struct wd_softc *wd)
226 int error;
227 uint8_t *buf = wd->sc_buf;
229 if ((error = atac_exec_identify(wd, buf)) != 0)
230 return error;
232 wd->sc_params = *(struct ataparams *)buf;
234 /* 48-bit LBA addressing */
235 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0) {
236 DPRINTF(("Drive supports LBA48.\n"));
237 wd->sc_flags |= WDF_LBA48;
240 /* Prior to ATA-4, LBA was optional. */
241 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0) {
242 DPRINTF(("Drive supports LBA.\n"));
243 wd->sc_flags |= WDF_LBA;
246 return 0;
249 static int
250 wdgetdisklabel(struct wd_softc *wd)
252 char *msg;
253 int sector, i, n;
254 size_t rsize;
255 struct mbr_partition *dp, *bsdp;
256 struct disklabel *lp;
257 uint8_t *buf = wd->sc_buf;
259 lp = &wd->sc_label;
260 memset(lp, 0, sizeof(struct disklabel));
262 sector = 0;
263 if (wdstrategy(wd, F_READ, MBR_BBSECTOR, DEV_BSIZE, buf, &rsize))
264 return EOFFSET;
266 dp = (struct mbr_partition *)(buf + MBR_PART_OFFSET);
267 bsdp = NULL;
268 for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
269 if (dp->mbrp_type == MBR_PTYPE_NETBSD) {
270 bsdp = dp;
271 break;
274 if (!bsdp) {
275 /* generate fake disklabel */
276 lp->d_secsize = DEV_BSIZE;
277 lp->d_ntracks = wd->sc_params.atap_heads;
278 lp->d_nsectors = wd->sc_params.atap_sectors;
279 lp->d_ncylinders = wd->sc_params.atap_cylinders;
280 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
281 if (strcmp((char *)wd->sc_params.atap_model, "ST506") == 0)
282 lp->d_type = DTYPE_ST506;
283 else
284 lp->d_type = DTYPE_ESDI;
285 strncpy(lp->d_typename, (char *)wd->sc_params.atap_model, 16);
286 strncpy(lp->d_packname, "fictitious", 16);
287 if (wd->sc_capacity > UINT32_MAX)
288 lp->d_secperunit = UINT32_MAX;
289 else
290 lp->d_secperunit = wd->sc_capacity;
291 lp->d_rpm = 3600;
292 lp->d_interleave = 1;
293 lp->d_flags = 0;
294 lp->d_partitions[RAW_PART].p_offset = 0;
295 lp->d_partitions[RAW_PART].p_size =
296 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
297 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
298 lp->d_magic = DISKMAGIC;
299 lp->d_magic2 = DISKMAGIC;
300 lp->d_checksum = dkcksum(lp);
302 dp = (struct mbr_partition *)(buf + MBR_PART_OFFSET);
303 n = 'e' - 'a';
304 for (i = 0; i < MBR_PART_COUNT; i++, dp++) {
305 if (dp->mbrp_type == MBR_PTYPE_UNUSED)
306 continue;
307 lp->d_partitions[n].p_offset = bswap32(dp->mbrp_start);
308 lp->d_partitions[n].p_size = bswap32(dp->mbrp_size);
309 switch (dp->mbrp_type) {
310 case MBR_PTYPE_FAT12:
311 case MBR_PTYPE_FAT16S:
312 case MBR_PTYPE_FAT16B:
313 case MBR_PTYPE_FAT32:
314 case MBR_PTYPE_FAT32L:
315 case MBR_PTYPE_FAT16L:
316 lp->d_partitions[n].p_fstype = FS_MSDOS;
317 break;
318 case MBR_PTYPE_LNXEXT2:
319 lp->d_partitions[n].p_fstype = FS_EX2FS;
320 break;
321 default:
322 lp->d_partitions[n].p_fstype = FS_OTHER;
323 break;
325 n += 1;
327 lp->d_npartitions = n;
329 else {
330 sector = bswap32(bsdp->mbrp_start);
331 if (wdstrategy(wd, F_READ, sector + LABELSECTOR, DEV_BSIZE,
332 buf, &rsize))
333 return EOFFSET;
334 msg = getdisklabel((char *)buf + LABELOFFSET, &wd->sc_label);
335 if (msg != NULL)
336 printf("wd%d: getdisklabel: %s\n", wd->sc_unit, msg);
339 DPRINTF(("label info: d_secsize %d, d_nsectors %d, d_ncylinders %d,"
340 "d_ntracks %d, d_secpercyl %d\n",
341 wd->sc_label.d_secsize,
342 wd->sc_label.d_nsectors,
343 wd->sc_label.d_ncylinders,
344 wd->sc_label.d_ntracks,
345 wd->sc_label.d_secpercyl));
347 return 0;
350 static int
351 atac_wait_for_ready(struct atac_channel *chp)
353 u_int timeout;
355 for (timeout = WDC_TIMEOUT; timeout > 0; --timeout) {
356 if ((WDC_READ_CMD(chp, wd_status) & (WDCS_BSY | WDCS_DRDY))
357 == WDCS_DRDY)
358 return 0;
360 return ENXIO;
363 static int
364 atac_read_block(struct wd_softc *wd, struct atac_command *cmd)
366 int i;
367 struct atac_channel *chp = wd->sc_channel;
368 uint16_t *ptr = (uint16_t *)cmd->data;
370 if (ptr == NULL)
371 return 0;
372 for (i = cmd->bcount; i > 0; i -= sizeof(uint16_t))
373 *ptr++ = WDC_READ_DATA(chp);
374 return 0;
377 static int
378 atacommand(struct wd_softc *wd, struct atac_command *cmd)
380 struct atac_channel *chp = wd->sc_channel;
382 if (wd->sc_flags & WDF_LBA48) {
383 WDC_WRITE_CMD(chp, wd_sdh, (cmd->drive << 4) | WDSD_LBA);
385 /* previous */
386 WDC_WRITE_CMD(chp, wd_features, 0);
387 WDC_WRITE_CMD(chp, wd_seccnt, cmd->r_count >> 8);
388 WDC_WRITE_CMD(chp, wd_lba_hi, cmd->r_blkno >> 40);
389 WDC_WRITE_CMD(chp, wd_lba_mi, cmd->r_blkno >> 32);
390 WDC_WRITE_CMD(chp, wd_lba_lo, cmd->r_blkno >> 24);
392 /* current */
393 WDC_WRITE_CMD(chp, wd_features, 0);
394 WDC_WRITE_CMD(chp, wd_seccnt, cmd->r_count);
395 WDC_WRITE_CMD(chp, wd_lba_hi, cmd->r_blkno >> 16);
396 WDC_WRITE_CMD(chp, wd_lba_mi, cmd->r_blkno >> 8);
397 WDC_WRITE_CMD(chp, wd_lba_lo, cmd->r_blkno);
399 /* Send command. */
400 WDC_WRITE_CMD(chp, wd_command, cmd->r_command);
402 if (atac_wait_for_ready(chp) != 0)
403 return ENXIO;
405 if (WDC_READ_CMD(chp, wd_status) & WDCS_ERR) {
406 printf("wd%d: error %x\n", chp->compatchan,
407 WDC_READ_CMD(chp, wd_error));
408 return ENXIO;
411 else {
412 WDC_WRITE_CMD(chp, wd_precomp, cmd->r_precomp);
413 WDC_WRITE_CMD(chp, wd_seccnt, cmd->r_count);
414 WDC_WRITE_CMD(chp, wd_sector, cmd->r_sector);
415 WDC_WRITE_CMD(chp, wd_cyl_lo, cmd->r_cyl);
416 WDC_WRITE_CMD(chp, wd_cyl_hi, cmd->r_cyl >> 8);
417 WDC_WRITE_CMD(chp, wd_sdh,
418 WDSD_IBM | (cmd->drive << 4) | cmd->r_head);
419 WDC_WRITE_CMD(chp, wd_command, cmd->r_command);
422 if (atac_wait_for_ready(chp) != 0)
423 return ENXIO;
425 if (WDC_READ_CMD(chp, wd_status) & WDCS_ERR) {
426 printf("wd%d: error %x\n", chp->compatchan,
427 WDC_READ_CMD(chp, wd_error));
428 return ENXIO;
430 return 0;
433 static int
434 atac_exec_identify(struct wd_softc *wd, void *data)
436 int error;
437 struct atac_command *cmd;
439 cmd = &wd->sc_command;
440 memset(cmd, 0, sizeof(struct atac_command));
442 cmd->r_command = WDCC_IDENTIFY;
443 cmd->r_count = 1;
444 cmd->data = data;
445 cmd->drive = wd->sc_unit;
446 cmd->bcount = wd->sc_label.d_secsize;
448 if ((error = atacommand(wd, cmd)) != 0)
449 return error;
450 return atac_read_block(wd, cmd);
453 static int
454 atac_exec_read(struct wd_softc *wd, int exe, daddr_t blkno, void *data)
456 int error;
457 struct atac_command *cmd;
459 cmd = &wd->sc_command;
460 memset(cmd, 0, sizeof(struct atac_command));
462 if (wd->sc_flags & WDF_LBA48)
463 cmd->r_blkno = blkno;
464 else if (wd->sc_flags & WDF_LBA) {
465 cmd->r_sector = blkno & 0xff;
466 cmd->r_cyl = (blkno >> 8) & 0xffff;
467 cmd->r_head = (blkno >> 24) & 0x0f;
468 cmd->r_head |= WDSD_LBA;
470 else {
471 cmd->r_sector = 1 + (blkno % wd->sc_label.d_nsectors);
472 blkno /= wd->sc_label.d_nsectors;
473 cmd->r_head = blkno % wd->sc_label.d_ntracks;
474 blkno /= wd->sc_label.d_ntracks;
475 cmd->r_cyl = blkno;
476 cmd->r_head |= WDSD_CHS;
478 cmd->r_command = exe;
479 cmd->r_count = 1;
480 cmd->data = data;
481 cmd->drive = wd->sc_unit;
482 cmd->bcount = wd->sc_label.d_secsize;
484 if ((error = atacommand(wd, cmd)) != 0)
485 return error;
486 return atac_read_block(wd, cmd);