satasii.c 4.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2009 Rudolf Marek <r.marek@assembler.cz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 20 21 22
 */

/* Datasheets can be found on http://www.siliconimage.com. Great thanks! */

23
#include "programmer.h"
24
#include "hwaccess.h"
25 26 27

#define PCI_VENDOR_ID_SII	0x1095

28 29
#define SATASII_MEMMAP_SIZE	0x100

Stefan Tauner's avatar
Stefan Tauner committed
30
static uint8_t *sii_bar;
31
static uint16_t id;
32

33
const struct dev_entry satas_sii[] = {
34 35 36
	{0x1095, 0x0680, OK, "Silicon Image", "PCI0680 Ultra ATA-133 Host Ctrl"},
	{0x1095, 0x3112, OK, "Silicon Image", "SiI 3112 [SATALink/SATARaid] SATA Ctrl"},
	{0x1095, 0x3114, OK, "Silicon Image", "SiI 3114 [SATALink/SATARaid] SATA Ctrl"},
37
	{0x1095, 0x3124, OK, "Silicon Image", "SiI 3124 PCI-X SATA Ctrl"},
38
	{0x1095, 0x3132, OK, "Silicon Image", "SiI 3132 SATA Raid II Ctrl"},
39
	{0x1095, 0x3512, OK, "Silicon Image", "SiI 3512 [SATALink/SATARaid] SATA Ctrl"},
40

Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
41
	{0},
42 43
};

Stefan Tauner's avatar
Stefan Tauner committed
44 45
static void satasii_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
static uint8_t satasii_chip_readb(const struct flashctx *flash, const chipaddr addr);
46
static const struct par_master par_master_satasii = {
47 48 49 50 51 52 53 54 55 56
		.chip_readb		= satasii_chip_readb,
		.chip_readw		= fallback_chip_readw,
		.chip_readl		= fallback_chip_readl,
		.chip_readn		= fallback_chip_readn,
		.chip_writeb		= satasii_chip_writeb,
		.chip_writew		= fallback_chip_writew,
		.chip_writel		= fallback_chip_writel,
		.chip_writen		= fallback_chip_writen,
};

57 58 59 60 61 62 63 64 65 66 67 68 69 70
static uint32_t satasii_wait_done(void)
{
	uint32_t ctrl_reg;
	int i = 0;
	while ((ctrl_reg = pci_mmio_readl(sii_bar)) & (1 << 25)) {
		if (++i > 10000) {
			msg_perr("%s: control register stuck at %08x, ignoring.\n",
				 __func__, pci_mmio_readl(sii_bar));
			break;
		}
	}
	return ctrl_reg;
}

71 72
int satasii_init(void)
{
73
	struct pci_dev *dev = NULL;
74 75 76
	uint32_t addr;
	uint16_t reg_offset;

77 78
	if (rget_io_perms())
		return 1;
79

80 81 82
	dev = pcidev_init(satas_sii, PCI_BASE_ADDRESS_0);
	if (!dev)
		return 1;
83

84
	id = dev->device_id;
85 86

	if ((id == 0x3132) || (id == 0x3124)) {
87
		addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
88 89
		if (!addr)
			return 1;
90 91
		reg_offset = 0x70;
	} else {
92
		addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_5);
93 94
		if (!addr)
			return 1;
95 96 97
		reg_offset = 0x50;
	}

Stefan Tauner's avatar
Stefan Tauner committed
98 99 100 101
	sii_bar = rphysmap("SATA SiI registers", addr, SATASII_MEMMAP_SIZE);
	if (sii_bar == ERROR_PTR)
		return 1;
	sii_bar += reg_offset;
102

103
	/* Check if ROM cycle are OK. */
104
	if ((id != 0x0680) && (!(pci_mmio_readl(sii_bar) & (1 << 26))))
Stefan Tauner's avatar
Stefan Tauner committed
105
		msg_pwarn("Warning: Flash seems unconnected.\n");
106

107
	register_par_master(&par_master_satasii, BUS_PARALLEL);
108

109 110 111
	return 0;
}

Stefan Tauner's avatar
Stefan Tauner committed
112
static void satasii_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
113
{
114 115
	uint32_t data_reg;
	uint32_t ctrl_reg = satasii_wait_done();
116

117
	/* Mask out unused/reserved bits, set writes and start transaction. */
118 119 120
	ctrl_reg &= 0xfcf80000;
	ctrl_reg |= (1 << 25) | (0 << 24) | ((uint32_t) addr & 0x7ffff);

121 122 123
	data_reg = (pci_mmio_readl((sii_bar + 4)) & ~0xff) | val;
	pci_mmio_writel(data_reg, (sii_bar + 4));
	pci_mmio_writel(ctrl_reg, sii_bar);
124

125
	satasii_wait_done();
126 127
}

Stefan Tauner's avatar
Stefan Tauner committed
128
static uint8_t satasii_chip_readb(const struct flashctx *flash, const chipaddr addr)
129
{
130
	uint32_t ctrl_reg = satasii_wait_done();
131

132
	/* Mask out unused/reserved bits, set reads and start transaction. */
133 134 135
	ctrl_reg &= 0xfcf80000;
	ctrl_reg |= (1 << 25) | (1 << 24) | ((uint32_t) addr & 0x7ffff);

136
	pci_mmio_writel(ctrl_reg, sii_bar);
137

138
	satasii_wait_done();
139

140
	return (pci_mmio_readl(sii_bar + 4)) & 0xff;
141
}