blob: c057a2e8134476e8adf3f8df7fc1b74e6a09dc38 [file] [log] [blame]
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010,2011 Carl-Daniel Hailfinger
5 * Written by Carl-Daniel Hailfinger for Angelbird Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * 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/* Datasheets are not public (yet?) */
Andrew Morgana0743832011-07-25 22:07:05 +000022#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000023
24#include <stdlib.h>
25#include "flash.h"
26#include "programmer.h"
27
28uint8_t *mv_bar;
29uint16_t mv_iobar;
30
31const struct pcidev_status satas_mv[] = {
32 /* 88SX6041 and 88SX6042 are the same according to the datasheet. */
33 {0x11ab, 0x7042, OK, "Marvell", "88SX7042 PCI-e 4-port SATA-II"},
34
35 {},
36};
37
38#define NVRAM_PARAM 0x1045c
39#define FLASH_PARAM 0x1046c
40#define EXPANSION_ROM_BAR_CONTROL 0x00d2c
41#define PCI_BAR2_CONTROL 0x00c08
42#define GPIO_PORT_CONTROL 0x104f0
43
David Hendricks8bb20212011-06-14 01:35:36 +000044static int satamv_shutdown(void *data)
45{
46 physunmap(mv_bar, 0x20000);
47 pci_cleanup(pacc);
48 release_io_perms();
49 return 0;
50}
51
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000052/*
53 * Random notes:
54 * FCE# Flash Chip Enable
55 * FWE# Flash Write Enable
56 * FOE# Flash Output Enable
57 * FALE[1:0] Flash Address Latch Enable
58 * FAD[7:0] Flash Multiplexed Address/Data Bus
59 * FA[2:0] Flash Address Low
60 *
61 * GPIO[15,2] GPIO Port Mode
62 * GPIO[4:3] Flash Size
63 *
64 * 0xd2c Expansion ROM BAR Control
65 * 0xc08 PCI BAR2 (Flash/NVRAM) Control
66 * 0x1046c Flash Parameters
67 */
68int satamv_init(void)
69{
70 uintptr_t addr;
71 uint32_t tmp;
72
73 get_io_perms();
74
75 /* BAR0 has all internal registers memory mapped. */
76 /* No need to check for errors, pcidev_init() will not return in case
77 * of errors.
78 */
Carl-Daniel Hailfinger40446ee2011-03-07 01:08:09 +000079 addr = pcidev_init(PCI_BASE_ADDRESS_0, satas_mv);
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000080
81 mv_bar = physmap("Marvell 88SX7042 registers", addr, 0x20000);
82 if (mv_bar == ERROR_PTR)
83 goto error_out;
84
David Hendricks8bb20212011-06-14 01:35:36 +000085 if (register_shutdown(satamv_shutdown, NULL))
86 return 1;
87
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000088 tmp = pci_mmio_readl(mv_bar + FLASH_PARAM);
89 msg_pspew("Flash Parameters:\n");
90 msg_pspew("TurnOff=0x%01x\n", (tmp >> 0) & 0x7);
91 msg_pspew("Acc2First=0x%01x\n", (tmp >> 3) & 0xf);
92 msg_pspew("Acc2Next=0x%01x\n", (tmp >> 7) & 0xf);
93 msg_pspew("ALE2Wr=0x%01x\n", (tmp >> 11) & 0x7);
94 msg_pspew("WrLow=0x%01x\n", (tmp >> 14) & 0x7);
95 msg_pspew("WrHigh=0x%01x\n", (tmp >> 17) & 0x7);
96 msg_pspew("Reserved[21:20]=0x%01x\n", (tmp >> 20) & 0x3);
97 msg_pspew("TurnOffExt=0x%01x\n", (tmp >> 22) & 0x1);
98 msg_pspew("Acc2FirstExt=0x%01x\n", (tmp >> 23) & 0x1);
99 msg_pspew("Acc2NextExt=0x%01x\n", (tmp >> 24) & 0x1);
100 msg_pspew("ALE2WrExt=0x%01x\n", (tmp >> 25) & 0x1);
101 msg_pspew("WrLowExt=0x%01x\n", (tmp >> 26) & 0x1);
102 msg_pspew("WrHighExt=0x%01x\n", (tmp >> 27) & 0x1);
103 msg_pspew("Reserved[31:28]=0x%01x\n", (tmp >> 28) & 0xf);
104
105 tmp = pci_mmio_readl(mv_bar + EXPANSION_ROM_BAR_CONTROL);
106 msg_pspew("Expansion ROM BAR Control:\n");
107 msg_pspew("ExpROMSz=0x%01x\n", (tmp >> 19) & 0x7);
108
109 /* Enable BAR2 mapping to flash */
110 tmp = pci_mmio_readl(mv_bar + PCI_BAR2_CONTROL);
111 msg_pspew("PCI BAR2 (Flash/NVRAM) Control:\n");
112 msg_pspew("Bar2En=0x%01x\n", (tmp >> 0) & 0x1);
113 msg_pspew("BAR2TransAttr=0x%01x\n", (tmp >> 1) & 0x1f);
114 msg_pspew("BAR2Sz=0x%01x\n", (tmp >> 19) & 0x7);
115 tmp &= 0xffffffc0;
116 tmp |= 0x0000001f;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000117 pci_rmmio_writel(tmp, mv_bar + PCI_BAR2_CONTROL);
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000118
119 /* Enable flash: GPIO Port Control Register 0x104f0 */
120 tmp = pci_mmio_readl(mv_bar + GPIO_PORT_CONTROL);
121 msg_pspew("GPIOPortMode=0x%01x\n", (tmp >> 0) & 0x3);
122 if (((tmp >> 0) & 0x3) != 0x2)
123 msg_pinfo("Warning! Either the straps are incorrect or you "
124 "have no flash or someone overwrote the strap "
125 "values!\n");
126 tmp &= 0xfffffffc;
127 tmp |= 0x2;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000128 pci_rmmio_writel(tmp, mv_bar + GPIO_PORT_CONTROL);
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000129
130 /* Get I/O BAR location. */
131 tmp = pci_read_long(pcidev_dev, PCI_BASE_ADDRESS_2) &
132 PCI_BASE_ADDRESS_IO_MASK;
133 /* Truncate to reachable range.
134 * FIXME: Check if the I/O BAR is actually reachable.
135 * This is an arch specific check.
136 */
137 mv_iobar = tmp & 0xffff;
138 msg_pspew("Activating I/O BAR at 0x%04x\n", mv_iobar);
139
140 buses_supported = CHIP_BUSTYPE_PARALLEL;
141
142 /* 512 kByte with two 8-bit latches, and
143 * 4 MByte with additional 3-bit latch. */
144 max_rom_decode.parallel = 4 * 1024 * 1024;
145
146 return 0;
147
148error_out:
149 pci_cleanup(pacc);
150 release_io_perms();
151 return 1;
152}
153
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000154/* BAR2 (MEM) can map NVRAM and flash. We set it to flash in the init function.
155 * If BAR2 is disabled, it still can be accessed indirectly via BAR1 (I/O).
156 * This code only supports indirect accesses for now.
157 */
158
159/* Indirect access to via the I/O BAR1. */
160static void satamv_indirect_chip_writeb(uint8_t val, chipaddr addr)
161{
162 /* 0x80000000 selects BAR2 for remapping. */
163 OUTL(((uint32_t)addr | 0x80000000) & 0xfffffffc, mv_iobar);
164 OUTB(val, mv_iobar + 0x80 + (addr & 0x3));
165}
166
167/* Indirect access to via the I/O BAR1. */
168static uint8_t satamv_indirect_chip_readb(const chipaddr addr)
169{
170 /* 0x80000000 selects BAR2 for remapping. */
171 OUTL(((uint32_t)addr | 0x80000000) & 0xfffffffc, mv_iobar);
172 return INB(mv_iobar + 0x80 + (addr & 0x3));
173}
174
175/* FIXME: Prefer direct access to BAR2 if BAR2 is active. */
176void satamv_chip_writeb(uint8_t val, chipaddr addr)
177{
178 satamv_indirect_chip_writeb(val, addr);
179}
180
181/* FIXME: Prefer direct access to BAR2 if BAR2 is active. */
182uint8_t satamv_chip_readb(const chipaddr addr)
183{
184 return satamv_indirect_chip_readb(addr);
185}
Andrew Morgana0743832011-07-25 22:07:05 +0000186
187#else
188#error PCI port I/O access is not supported on this architecture yet.
189#endif