Home | History | Annotate | Line # | Download | only in altboot
      1  1.7       phx /* $NetBSD: siisata.c,v 1.7 2017/08/03 19:22:15 phx Exp $ */
      2  1.1  nisimura 
      3  1.1  nisimura /*-
      4  1.1  nisimura  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.1  nisimura  * All rights reserved.
      6  1.1  nisimura  *
      7  1.1  nisimura  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  nisimura  * by Tohru Nishimura.
      9  1.1  nisimura  *
     10  1.1  nisimura  * Redistribution and use in source and binary forms, with or without
     11  1.1  nisimura  * modification, are permitted provided that the following conditions
     12  1.1  nisimura  * are met:
     13  1.1  nisimura  * 1. Redistributions of source code must retain the above copyright
     14  1.1  nisimura  *    notice, this list of conditions and the following disclaimer.
     15  1.1  nisimura  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  nisimura  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  nisimura  *    documentation and/or other materials provided with the distribution.
     18  1.1  nisimura  *
     19  1.1  nisimura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  nisimura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  nisimura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  nisimura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  nisimura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  nisimura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  nisimura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  nisimura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  nisimura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  nisimura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  nisimura  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  nisimura  */
     31  1.1  nisimura 
     32  1.1  nisimura #include <sys/param.h>
     33  1.1  nisimura 
     34  1.1  nisimura #include <lib/libsa/stand.h>
     35  1.1  nisimura 
     36  1.1  nisimura #include "globals.h"
     37  1.1  nisimura 
     38  1.5       phx /*
     39  1.5       phx  * - no vtophys() translation, vaddr_t == paddr_t.
     40  1.5       phx  */
     41  1.5       phx #define CSR_READ_4(r)		in32rb(r)
     42  1.5       phx #define CSR_WRITE_4(r,v)	out32rb(r,v)
     43  1.5       phx 
     44  1.5       phx static int satapresense(struct dkdev_ata *, int);
     45  1.5       phx 
     46  1.1  nisimura static uint32_t pciiobase = PCI_XIOBASE;
     47  1.1  nisimura 
     48  1.6       phx int sata_delay[4] = { 3, 3, 3, 3 };	/* drive power-up delay per channel */
     49  1.6       phx 
     50  1.1  nisimura int
     51  1.1  nisimura siisata_match(unsigned tag, void *data)
     52  1.1  nisimura {
     53  1.1  nisimura 	unsigned v;
     54  1.1  nisimura 
     55  1.1  nisimura 	v = pcicfgread(tag, PCI_ID_REG);
     56  1.1  nisimura 	switch (v) {
     57  1.1  nisimura 	case PCI_DEVICE(0x1095, 0x3112): /* SiI 3112 SATALink */
     58  1.1  nisimura 	case PCI_DEVICE(0x1095, 0x3512): /*     3512 SATALink */
     59  1.1  nisimura 	case PCI_DEVICE(0x1095, 0x3114): /* SiI 3114 SATALink */
     60  1.1  nisimura 		return 1;
     61  1.1  nisimura 	}
     62  1.1  nisimura 	return 0;
     63  1.1  nisimura }
     64  1.1  nisimura 
     65  1.1  nisimura void *
     66  1.1  nisimura siisata_init(unsigned tag, void *data)
     67  1.1  nisimura {
     68  1.1  nisimura 	unsigned idreg;
     69  1.5       phx 	int n, nchan, retries/*waitforspinup*/;
     70  1.1  nisimura 	struct dkdev_ata *l;
     71  1.1  nisimura 
     72  1.1  nisimura 	l = alloc(sizeof(struct dkdev_ata));
     73  1.1  nisimura 	memset(l, 0, sizeof(struct dkdev_ata));
     74  1.1  nisimura 	l->iobuf = allocaligned(512, 16);
     75  1.1  nisimura 	l->tag = tag;
     76  1.1  nisimura 
     77  1.1  nisimura 	idreg = pcicfgread(tag, PCI_ID_REG);
     78  1.1  nisimura 	l->bar[0] = pciiobase + (pcicfgread(tag, 0x10) &~ 01);
     79  1.1  nisimura 	l->bar[1] = pciiobase + (pcicfgread(tag, 0x14) &~ 01);
     80  1.1  nisimura 	l->bar[2] = pciiobase + (pcicfgread(tag, 0x18) &~ 01);
     81  1.1  nisimura 	l->bar[3] = pciiobase + (pcicfgread(tag, 0x1c) &~ 01);
     82  1.1  nisimura 	l->bar[4] = pciiobase + (pcicfgread(tag, 0x20) &~ 01);
     83  1.1  nisimura 	l->bar[5] = pcicfgread(tag, 0x24) &~ 0x3ff;
     84  1.1  nisimura 
     85  1.1  nisimura 	if ((PCI_PRODUCT(idreg) & 0xf) == 0x2) {
     86  1.1  nisimura 		/* 3112/3512 */
     87  1.1  nisimura 		l->chan[0].cmd = l->bar[0];
     88  1.1  nisimura 		l->chan[0].ctl = l->chan[0].alt = l->bar[1] | 02;
     89  1.1  nisimura 		l->chan[0].dma = l->bar[4] + 0x0;
     90  1.1  nisimura 		l->chan[1].cmd = l->bar[2];
     91  1.1  nisimura 		l->chan[1].ctl = l->chan[1].alt = l->bar[3] | 02;
     92  1.1  nisimura 		l->chan[1].dma = l->bar[4] + 0x8;
     93  1.1  nisimura 		nchan = 2;
     94  1.1  nisimura 	}
     95  1.1  nisimura 	else {
     96  1.1  nisimura 		/* 3114 - assume BA5 access is possible XXX */
     97  1.1  nisimura 		l->chan[0].cmd = l->bar[5] + 0x080;
     98  1.1  nisimura 		l->chan[0].ctl = l->chan[0].alt = (l->bar[5] + 0x088) | 02;
     99  1.1  nisimura 		l->chan[1].cmd = l->bar[5] + 0x0c0;
    100  1.1  nisimura 		l->chan[1].ctl = l->chan[1].alt = (l->bar[5] + 0x0c8) | 02;
    101  1.1  nisimura 		l->chan[2].cmd = l->bar[5] + 0x280;
    102  1.1  nisimura 		l->chan[2].ctl = l->chan[2].alt = (l->bar[5] + 0x288) | 02;
    103  1.1  nisimura 		l->chan[3].cmd = l->bar[5] + 0x2c0;
    104  1.1  nisimura 		l->chan[3].ctl = l->chan[3].alt = (l->bar[5] + 0x2c8) | 02;
    105  1.1  nisimura 		nchan = 4;
    106  1.1  nisimura 	}
    107  1.1  nisimura 
    108  1.1  nisimura 	/* configure PIO transfer mode */
    109  1.1  nisimura 	pcicfgwrite(tag, 0x80, 0x00);
    110  1.1  nisimura 	pcicfgwrite(tag, 0x84, 0x00);
    111  1.1  nisimura 
    112  1.5       phx 	for (n = 0; n < nchan; n++) {
    113  1.5       phx 		l->presense[n] = satapresense(l, n);
    114  1.5       phx 		if (l->presense[n] == 0) {
    115  1.6       phx 			/* wait some seconds to power-up the drive */
    116  1.6       phx 			for (retries = 0; retries < sata_delay[n]; retries++) {
    117  1.6       phx 				wakeup_drive(l, n);
    118  1.6       phx 				printf("Waiting %2d seconds for powering up "
    119  1.6       phx 				    "port %d.\r", sata_delay[n] - retries, n);
    120  1.6       phx 				delay(1000 * 1000);
    121  1.6       phx 				if ((l->presense[n] = satapresense(l, n)) != 0)
    122  1.6       phx 					break;
    123  1.6       phx 			}
    124  1.6       phx 			putchar('\n');
    125  1.6       phx 			if (l->presense[n] == 0) {
    126  1.6       phx 				DPRINTF(("port %d not present\n", n));
    127  1.6       phx 				continue;
    128  1.7       phx 			} else {
    129  1.7       phx 				/*
    130  1.7       phx 				 * XXX perform_atareset() does not work
    131  1.7       phx 				 * when the drive is not completely spun up?
    132  1.7       phx 				 * So insert another delay here.
    133  1.7       phx 				 */
    134  1.7       phx 				printf("Waiting 15 seconds for port %d "
    135  1.7       phx 				    "to spin up.\n", n);
    136  1.7       phx 				delay(15 * 1000 * 1000);
    137  1.6       phx 			}
    138  1.5       phx 		}
    139  1.5       phx 		if (atachkpwr(l, n) != ATA_PWR_ACTIVE) {
    140  1.5       phx 			/* drive is probably sleeping, wake it up */
    141  1.6       phx 			for (retries = 0; retries < 20; retries++) {
    142  1.5       phx 				wakeup_drive(l, n);
    143  1.5       phx 				DPRINTF(("port %d spinning up...\n", n));
    144  1.5       phx 				delay(1000 * 1000);
    145  1.5       phx 				l->presense[n] = perform_atareset(l, n);
    146  1.5       phx 				if (atachkpwr(l, n) == ATA_PWR_ACTIVE)
    147  1.5       phx 					break;
    148  1.5       phx 			}
    149  1.5       phx 		} else {
    150  1.5       phx 			/* check to see whether soft reset works */
    151  1.5       phx 			DPRINTF(("port %d active\n", n));
    152  1.6       phx 			for (retries = 0; retries < 20; retries++) {
    153  1.5       phx 				l->presense[n] = perform_atareset(l, n);
    154  1.5       phx 				if (l->presense[n] != 0)
    155  1.4       phx 					break;
    156  1.6       phx 				wakeup_drive(l, n);
    157  1.5       phx 				DPRINTF(("port %d cold-starting...\n", n));
    158  1.5       phx 				delay(1000 * 1000);
    159  1.1  nisimura 			}
    160  1.4       phx 		}
    161  1.5       phx 
    162  1.5       phx 		if (l->presense[n])
    163  1.5       phx 			printf("port %d present\n", n);
    164  1.1  nisimura 	}
    165  1.1  nisimura 	return l;
    166  1.1  nisimura }
    167  1.5       phx 
    168  1.5       phx static int
    169  1.5       phx satapresense(struct dkdev_ata *l, int n)
    170  1.5       phx {
    171  1.5       phx #define VND_CH(n) (((n&02)<<8)+((n&01)<<7))
    172  1.5       phx #define VND_SC(n) (0x100+VND_CH(n))
    173  1.5       phx #define VND_SS(n) (0x104+VND_CH(n))
    174  1.5       phx 
    175  1.5       phx 	uint32_t sc = l->bar[5] + VND_SC(n);
    176  1.5       phx 	uint32_t ss = l->bar[5] + VND_SS(n);
    177  1.5       phx 	unsigned val;
    178  1.5       phx 
    179  1.5       phx 	val = (00 << 4) | (03 << 8);	/* any speed, no pwrmgt */
    180  1.5       phx 	CSR_WRITE_4(sc, val | 01);	/* perform init */
    181  1.5       phx 	delay(50 * 1000);
    182  1.5       phx 	CSR_WRITE_4(sc, val);
    183  1.5       phx 	delay(50 * 1000);
    184  1.5       phx 	val = CSR_READ_4(ss);		/* has completed */
    185  1.5       phx 	return ((val & 03) == 03);	/* active drive found */
    186  1.5       phx }
    187