Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 1 | /* |
| 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 Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* Datasheets are not public (yet?) */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include "flash.h" |
| 21 | #include "programmer.h" |
Thomas Heijligen | a065520 | 2021-12-14 16:36:05 +0100 | [diff] [blame] | 22 | #include "hwaccess_x86_io.h" |
Thomas Heijligen | 74b4aa0 | 2021-12-14 17:52:30 +0100 | [diff] [blame] | 23 | #include "hwaccess_physmap.h" |
Thomas Heijligen | d96c97c | 2021-11-02 21:03:00 +0100 | [diff] [blame] | 24 | #include "platform/pci.h" |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 25 | |
Jacob Garber | afc3ad6 | 2019-06-24 16:05:28 -0600 | [diff] [blame] | 26 | static uint8_t *mv_bar; |
| 27 | static uint16_t mv_iobar; |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 28 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 29 | static const struct dev_entry satas_mv[] = { |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 30 | /* 88SX6041 and 88SX6042 are the same according to the datasheet. */ |
| 31 | {0x11ab, 0x7042, OK, "Marvell", "88SX7042 PCI-e 4-port SATA-II"}, |
| 32 | |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 33 | {0}, |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | #define NVRAM_PARAM 0x1045c |
| 37 | #define FLASH_PARAM 0x1046c |
| 38 | #define EXPANSION_ROM_BAR_CONTROL 0x00d2c |
| 39 | #define PCI_BAR2_CONTROL 0x00c08 |
| 40 | #define GPIO_PORT_CONTROL 0x104f0 |
| 41 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 42 | static void satamv_chip_writeb(const struct flashctx *flash, uint8_t val, |
| 43 | chipaddr addr); |
| 44 | static uint8_t satamv_chip_readb(const struct flashctx *flash, |
| 45 | const chipaddr addr); |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 46 | static const struct par_master par_master_satamv = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 47 | .chip_readb = satamv_chip_readb, |
| 48 | .chip_readw = fallback_chip_readw, |
| 49 | .chip_readl = fallback_chip_readl, |
| 50 | .chip_readn = fallback_chip_readn, |
| 51 | .chip_writeb = satamv_chip_writeb, |
| 52 | .chip_writew = fallback_chip_writew, |
| 53 | .chip_writel = fallback_chip_writel, |
| 54 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 57 | /* |
| 58 | * Random notes: |
| 59 | * FCE# Flash Chip Enable |
| 60 | * FWE# Flash Write Enable |
| 61 | * FOE# Flash Output Enable |
| 62 | * FALE[1:0] Flash Address Latch Enable |
| 63 | * FAD[7:0] Flash Multiplexed Address/Data Bus |
| 64 | * FA[2:0] Flash Address Low |
| 65 | * |
| 66 | * GPIO[15,2] GPIO Port Mode |
| 67 | * GPIO[4:3] Flash Size |
| 68 | * |
| 69 | * 0xd2c Expansion ROM BAR Control |
| 70 | * 0xc08 PCI BAR2 (Flash/NVRAM) Control |
| 71 | * 0x1046c Flash Parameters |
| 72 | */ |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 73 | static int satamv_init(struct flashprog_programmer *const prog) |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 74 | { |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 75 | struct pci_dev *dev = NULL; |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 76 | uintptr_t addr; |
| 77 | uint32_t tmp; |
| 78 | |
Carl-Daniel Hailfinger | d6bb828 | 2012-07-21 17:27:08 +0000 | [diff] [blame] | 79 | if (rget_io_perms()) |
| 80 | return 1; |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 81 | |
| 82 | /* BAR0 has all internal registers memory mapped. */ |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 83 | dev = pcidev_init(satas_mv, PCI_BASE_ADDRESS_0); |
| 84 | if (!dev) |
| 85 | return 1; |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 86 | |
Carl-Daniel Hailfinger | a2faddf | 2013-01-05 23:52:45 +0000 | [diff] [blame] | 87 | addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0); |
Niklas Söderlund | 89edf36 | 2013-08-23 23:29:23 +0000 | [diff] [blame] | 88 | if (!addr) |
| 89 | return 1; |
| 90 | |
Stefan Tauner | 7fb5aa0 | 2013-08-14 15:48:44 +0000 | [diff] [blame] | 91 | mv_bar = rphysmap("Marvell 88SX7042 registers", addr, 0x20000); |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 92 | if (mv_bar == ERROR_PTR) |
Stefan Tauner | 5561955 | 2013-01-04 22:24:58 +0000 | [diff] [blame] | 93 | return 1; |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 94 | |
| 95 | tmp = pci_mmio_readl(mv_bar + FLASH_PARAM); |
| 96 | msg_pspew("Flash Parameters:\n"); |
| 97 | msg_pspew("TurnOff=0x%01x\n", (tmp >> 0) & 0x7); |
| 98 | msg_pspew("Acc2First=0x%01x\n", (tmp >> 3) & 0xf); |
| 99 | msg_pspew("Acc2Next=0x%01x\n", (tmp >> 7) & 0xf); |
| 100 | msg_pspew("ALE2Wr=0x%01x\n", (tmp >> 11) & 0x7); |
| 101 | msg_pspew("WrLow=0x%01x\n", (tmp >> 14) & 0x7); |
| 102 | msg_pspew("WrHigh=0x%01x\n", (tmp >> 17) & 0x7); |
| 103 | msg_pspew("Reserved[21:20]=0x%01x\n", (tmp >> 20) & 0x3); |
| 104 | msg_pspew("TurnOffExt=0x%01x\n", (tmp >> 22) & 0x1); |
| 105 | msg_pspew("Acc2FirstExt=0x%01x\n", (tmp >> 23) & 0x1); |
| 106 | msg_pspew("Acc2NextExt=0x%01x\n", (tmp >> 24) & 0x1); |
| 107 | msg_pspew("ALE2WrExt=0x%01x\n", (tmp >> 25) & 0x1); |
| 108 | msg_pspew("WrLowExt=0x%01x\n", (tmp >> 26) & 0x1); |
| 109 | msg_pspew("WrHighExt=0x%01x\n", (tmp >> 27) & 0x1); |
| 110 | msg_pspew("Reserved[31:28]=0x%01x\n", (tmp >> 28) & 0xf); |
| 111 | |
| 112 | tmp = pci_mmio_readl(mv_bar + EXPANSION_ROM_BAR_CONTROL); |
| 113 | msg_pspew("Expansion ROM BAR Control:\n"); |
| 114 | msg_pspew("ExpROMSz=0x%01x\n", (tmp >> 19) & 0x7); |
| 115 | |
| 116 | /* Enable BAR2 mapping to flash */ |
| 117 | tmp = pci_mmio_readl(mv_bar + PCI_BAR2_CONTROL); |
| 118 | msg_pspew("PCI BAR2 (Flash/NVRAM) Control:\n"); |
| 119 | msg_pspew("Bar2En=0x%01x\n", (tmp >> 0) & 0x1); |
| 120 | msg_pspew("BAR2TransAttr=0x%01x\n", (tmp >> 1) & 0x1f); |
| 121 | msg_pspew("BAR2Sz=0x%01x\n", (tmp >> 19) & 0x7); |
| 122 | tmp &= 0xffffffc0; |
| 123 | tmp |= 0x0000001f; |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 124 | pci_rmmio_writel(tmp, mv_bar + PCI_BAR2_CONTROL); |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 125 | |
| 126 | /* Enable flash: GPIO Port Control Register 0x104f0 */ |
| 127 | tmp = pci_mmio_readl(mv_bar + GPIO_PORT_CONTROL); |
| 128 | msg_pspew("GPIOPortMode=0x%01x\n", (tmp >> 0) & 0x3); |
| 129 | if (((tmp >> 0) & 0x3) != 0x2) |
| 130 | msg_pinfo("Warning! Either the straps are incorrect or you " |
| 131 | "have no flash or someone overwrote the strap " |
| 132 | "values!\n"); |
| 133 | tmp &= 0xfffffffc; |
| 134 | tmp |= 0x2; |
Carl-Daniel Hailfinger | 54ce73a | 2011-05-03 21:49:41 +0000 | [diff] [blame] | 135 | pci_rmmio_writel(tmp, mv_bar + GPIO_PORT_CONTROL); |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 136 | |
| 137 | /* Get I/O BAR location. */ |
Stefan Reinauer | 789ea5e | 2014-04-26 16:12:15 +0000 | [diff] [blame] | 138 | addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_2); |
Niklas Söderlund | 89edf36 | 2013-08-23 23:29:23 +0000 | [diff] [blame] | 139 | if (!addr) |
| 140 | return 1; |
| 141 | |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 142 | /* Truncate to reachable range. |
| 143 | * FIXME: Check if the I/O BAR is actually reachable. |
| 144 | * This is an arch specific check. |
| 145 | */ |
Stefan Reinauer | 789ea5e | 2014-04-26 16:12:15 +0000 | [diff] [blame] | 146 | mv_iobar = addr & 0xffff; |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 147 | msg_pspew("Activating I/O BAR at 0x%04x\n", mv_iobar); |
| 148 | |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 149 | /* 512 kByte with two 8-bit latches, and |
| 150 | * 4 MByte with additional 3-bit latch. */ |
Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 151 | return register_par_master(&par_master_satamv, BUS_PARALLEL, 4*MiB, NULL); |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 154 | /* 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. */ |
| 160 | static 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. */ |
| 168 | static 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. */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 176 | static void satamv_chip_writeb(const struct flashctx *flash, uint8_t val, |
| 177 | chipaddr addr) |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 178 | { |
| 179 | satamv_indirect_chip_writeb(val, addr); |
| 180 | } |
| 181 | |
| 182 | /* FIXME: Prefer direct access to BAR2 if BAR2 is active. */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 183 | static uint8_t satamv_chip_readb(const struct flashctx *flash, |
| 184 | const chipaddr addr) |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 185 | { |
| 186 | return satamv_indirect_chip_readb(addr); |
| 187 | } |
Andrew Morgan | a074383 | 2011-07-25 22:07:05 +0000 | [diff] [blame] | 188 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 189 | const struct programmer_entry programmer_satamv = { |
| 190 | .name = "satamv", |
| 191 | .type = PCI, |
| 192 | .devs.dev = satas_mv, |
| 193 | .init = satamv_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 194 | }; |