blob: f1c38283b27be84d113e9517124b71baa19ec10d [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.
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000015 */
16
17/* Datasheets are not public (yet?) */
18
19#include <stdlib.h>
20#include "flash.h"
21#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000022#include "hwaccess.h"
Thomas Heijligena0655202021-12-14 16:36:05 +010023#include "hwaccess_x86_io.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010024#include "hwaccess_physmap.h"
Thomas Heijligend96c97c2021-11-02 21:03:00 +010025#include "platform/pci.h"
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000026
Jacob Garberafc3ad62019-06-24 16:05:28 -060027static uint8_t *mv_bar;
28static uint16_t mv_iobar;
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000029
Thomas Heijligencc853d82021-05-04 15:32:17 +020030static const struct dev_entry satas_mv[] = {
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000031 /* 88SX6041 and 88SX6042 are the same according to the datasheet. */
32 {0x11ab, 0x7042, OK, "Marvell", "88SX7042 PCI-e 4-port SATA-II"},
33
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000034 {0},
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000035};
36
37#define NVRAM_PARAM 0x1045c
38#define FLASH_PARAM 0x1046c
39#define EXPANSION_ROM_BAR_CONTROL 0x00d2c
40#define PCI_BAR2_CONTROL 0x00c08
41#define GPIO_PORT_CONTROL 0x104f0
42
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000043static void satamv_chip_writeb(const struct flashctx *flash, uint8_t val,
44 chipaddr addr);
45static uint8_t satamv_chip_readb(const struct flashctx *flash,
46 const chipaddr addr);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000047static const struct par_master par_master_satamv = {
Thomas Heijligen43040f22022-06-23 14:38:35 +020048 .chip_readb = satamv_chip_readb,
49 .chip_readw = fallback_chip_readw,
50 .chip_readl = fallback_chip_readl,
51 .chip_readn = fallback_chip_readn,
52 .chip_writeb = satamv_chip_writeb,
53 .chip_writew = fallback_chip_writew,
54 .chip_writel = fallback_chip_writel,
55 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000056};
57
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000058/*
59 * Random notes:
60 * FCE# Flash Chip Enable
61 * FWE# Flash Write Enable
62 * FOE# Flash Output Enable
63 * FALE[1:0] Flash Address Latch Enable
64 * FAD[7:0] Flash Multiplexed Address/Data Bus
65 * FA[2:0] Flash Address Low
66 *
67 * GPIO[15,2] GPIO Port Mode
68 * GPIO[4:3] Flash Size
69 *
70 * 0xd2c Expansion ROM BAR Control
71 * 0xc08 PCI BAR2 (Flash/NVRAM) Control
72 * 0x1046c Flash Parameters
73 */
Thomas Heijligencc853d82021-05-04 15:32:17 +020074static int satamv_init(void)
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000075{
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +000076 struct pci_dev *dev = NULL;
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000077 uintptr_t addr;
78 uint32_t tmp;
79
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +000080 if (rget_io_perms())
81 return 1;
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000082
83 /* BAR0 has all internal registers memory mapped. */
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +000084 dev = pcidev_init(satas_mv, PCI_BASE_ADDRESS_0);
85 if (!dev)
86 return 1;
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000087
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +000088 addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
Niklas Söderlund89edf362013-08-23 23:29:23 +000089 if (!addr)
90 return 1;
91
Stefan Tauner7fb5aa02013-08-14 15:48:44 +000092 mv_bar = rphysmap("Marvell 88SX7042 registers", addr, 0x20000);
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000093 if (mv_bar == ERROR_PTR)
Stefan Tauner55619552013-01-04 22:24:58 +000094 return 1;
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +000095
96 tmp = pci_mmio_readl(mv_bar + FLASH_PARAM);
97 msg_pspew("Flash Parameters:\n");
98 msg_pspew("TurnOff=0x%01x\n", (tmp >> 0) & 0x7);
99 msg_pspew("Acc2First=0x%01x\n", (tmp >> 3) & 0xf);
100 msg_pspew("Acc2Next=0x%01x\n", (tmp >> 7) & 0xf);
101 msg_pspew("ALE2Wr=0x%01x\n", (tmp >> 11) & 0x7);
102 msg_pspew("WrLow=0x%01x\n", (tmp >> 14) & 0x7);
103 msg_pspew("WrHigh=0x%01x\n", (tmp >> 17) & 0x7);
104 msg_pspew("Reserved[21:20]=0x%01x\n", (tmp >> 20) & 0x3);
105 msg_pspew("TurnOffExt=0x%01x\n", (tmp >> 22) & 0x1);
106 msg_pspew("Acc2FirstExt=0x%01x\n", (tmp >> 23) & 0x1);
107 msg_pspew("Acc2NextExt=0x%01x\n", (tmp >> 24) & 0x1);
108 msg_pspew("ALE2WrExt=0x%01x\n", (tmp >> 25) & 0x1);
109 msg_pspew("WrLowExt=0x%01x\n", (tmp >> 26) & 0x1);
110 msg_pspew("WrHighExt=0x%01x\n", (tmp >> 27) & 0x1);
111 msg_pspew("Reserved[31:28]=0x%01x\n", (tmp >> 28) & 0xf);
112
113 tmp = pci_mmio_readl(mv_bar + EXPANSION_ROM_BAR_CONTROL);
114 msg_pspew("Expansion ROM BAR Control:\n");
115 msg_pspew("ExpROMSz=0x%01x\n", (tmp >> 19) & 0x7);
116
117 /* Enable BAR2 mapping to flash */
118 tmp = pci_mmio_readl(mv_bar + PCI_BAR2_CONTROL);
119 msg_pspew("PCI BAR2 (Flash/NVRAM) Control:\n");
120 msg_pspew("Bar2En=0x%01x\n", (tmp >> 0) & 0x1);
121 msg_pspew("BAR2TransAttr=0x%01x\n", (tmp >> 1) & 0x1f);
122 msg_pspew("BAR2Sz=0x%01x\n", (tmp >> 19) & 0x7);
123 tmp &= 0xffffffc0;
124 tmp |= 0x0000001f;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000125 pci_rmmio_writel(tmp, mv_bar + PCI_BAR2_CONTROL);
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000126
127 /* Enable flash: GPIO Port Control Register 0x104f0 */
128 tmp = pci_mmio_readl(mv_bar + GPIO_PORT_CONTROL);
129 msg_pspew("GPIOPortMode=0x%01x\n", (tmp >> 0) & 0x3);
130 if (((tmp >> 0) & 0x3) != 0x2)
131 msg_pinfo("Warning! Either the straps are incorrect or you "
132 "have no flash or someone overwrote the strap "
133 "values!\n");
134 tmp &= 0xfffffffc;
135 tmp |= 0x2;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000136 pci_rmmio_writel(tmp, mv_bar + GPIO_PORT_CONTROL);
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000137
138 /* Get I/O BAR location. */
Stefan Reinauer789ea5e2014-04-26 16:12:15 +0000139 addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_2);
Niklas Söderlund89edf362013-08-23 23:29:23 +0000140 if (!addr)
141 return 1;
142
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000143 /* Truncate to reachable range.
144 * FIXME: Check if the I/O BAR is actually reachable.
145 * This is an arch specific check.
146 */
Stefan Reinauer789ea5e2014-04-26 16:12:15 +0000147 mv_iobar = addr & 0xffff;
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000148 msg_pspew("Activating I/O BAR at 0x%04x\n", mv_iobar);
149
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000150 /* 512 kByte with two 8-bit latches, and
151 * 4 MByte with additional 3-bit latch. */
152 max_rom_decode.parallel = 4 * 1024 * 1024;
Anastasia Klimchukc1f2a472021-08-27 15:47:46 +1000153 return register_par_master(&par_master_satamv, BUS_PARALLEL, NULL);
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000154}
155
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000156/* BAR2 (MEM) can map NVRAM and flash. We set it to flash in the init function.
157 * If BAR2 is disabled, it still can be accessed indirectly via BAR1 (I/O).
158 * This code only supports indirect accesses for now.
159 */
160
161/* Indirect access to via the I/O BAR1. */
162static void satamv_indirect_chip_writeb(uint8_t val, chipaddr addr)
163{
164 /* 0x80000000 selects BAR2 for remapping. */
165 OUTL(((uint32_t)addr | 0x80000000) & 0xfffffffc, mv_iobar);
166 OUTB(val, mv_iobar + 0x80 + (addr & 0x3));
167}
168
169/* Indirect access to via the I/O BAR1. */
170static uint8_t satamv_indirect_chip_readb(const chipaddr addr)
171{
172 /* 0x80000000 selects BAR2 for remapping. */
173 OUTL(((uint32_t)addr | 0x80000000) & 0xfffffffc, mv_iobar);
174 return INB(mv_iobar + 0x80 + (addr & 0x3));
175}
176
177/* FIXME: Prefer direct access to BAR2 if BAR2 is active. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000178static void satamv_chip_writeb(const struct flashctx *flash, uint8_t val,
179 chipaddr addr)
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000180{
181 satamv_indirect_chip_writeb(val, addr);
182}
183
184/* FIXME: Prefer direct access to BAR2 if BAR2 is active. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000185static uint8_t satamv_chip_readb(const struct flashctx *flash,
186 const chipaddr addr)
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000187{
188 return satamv_indirect_chip_readb(addr);
189}
Andrew Morgana0743832011-07-25 22:07:05 +0000190
Thomas Heijligencc853d82021-05-04 15:32:17 +0200191const struct programmer_entry programmer_satamv = {
192 .name = "satamv",
193 .type = PCI,
194 .devs.dev = satas_mv,
195 .init = satamv_init,
196 .map_flash_region = fallback_map,
197 .unmap_flash_region = fallback_unmap,
198 .delay = internal_delay,
199};