blob: 61058b9bee90019c8432df45ba227d76fc4baee1 [file] [log] [blame]
Nico Huber0e76d992023-01-12 20:22:55 +01001/*
2 * This file is part of the flashprog project.
3 *
Nico Huber404529d2025-01-30 23:14:14 +01004 * Copyright (C) 2014 Stefan Tauner
5 *
Nico Huber0e76d992023-01-12 20:22:55 +01006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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
17#include <stddef.h>
18#include <stdint.h>
19
20#include "flash.h"
21#include "programmer.h"
22
Nico Huber10337f72026-03-04 19:57:27 +010023#include "chipdrivers/memory_bus.h"
Nico Huber0e76d992023-01-12 20:22:55 +010024
25static void *programmer_map_flash_region(const struct flashctx *flash, const char *descr,
26 uintptr_t phys_addr, size_t len)
27{
28 void *ret;
Nico Huber9a11cbf2023-01-13 01:19:07 +010029 if (flash->mst.par->map_flash)
30 ret = flash->mst.par->map_flash(descr, phys_addr, len);
Nico Huber0e76d992023-01-12 20:22:55 +010031 else
32 ret = fallback_map(descr, phys_addr, len);
33 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
34 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
35 return ret;
36}
37
38static void programmer_unmap_flash_region(const struct flashctx *flash, void *virt_addr, size_t len)
39{
Nico Huber9a11cbf2023-01-13 01:19:07 +010040 if (flash->mst.par->unmap_flash)
41 flash->mst.par->unmap_flash(virt_addr, len);
Nico Huber0e76d992023-01-12 20:22:55 +010042 else
43 fallback_unmap(virt_addr, len);
44 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
45}
46
47int prepare_memory_access(struct flashctx *flash, enum preparation_steps prep)
48{
Nico Huber47aa85c2026-02-21 14:57:20 +010049 const struct par_master *const par = flash->mst.par;
50
Nico Huberb89c4522024-12-04 23:30:13 +010051 if (prep == PREPARE_POST_PROBE)
52 return 0;
53
Nico Huber0e76d992023-01-12 20:22:55 +010054 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
55 flash->virtual_memory = (chipaddr)ERROR_PTR;
56 flash->virtual_registers = (chipaddr)ERROR_PTR;
57
Nico Huber0e76d992023-01-12 20:22:55 +010058 const chipsize_t size = flash->chip->total_size * 1024;
Nico Huber47aa85c2026-02-21 14:57:20 +010059 const uintptr_t base = par->rom_base ? par->rom_base : (0xffffffff - size + 1);
Nico Huberb1974022023-01-12 13:13:12 +010060 void *const addr = programmer_map_flash_region(flash, flash->chip->name, base, size);
Nico Huber0e76d992023-01-12 20:22:55 +010061 if (addr == ERROR_PTR) {
62 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
63 flash->chip->name, PRIxPTR_WIDTH, base);
64 return 1;
65 }
66 flash->physical_memory = base;
67 flash->virtual_memory = (chipaddr)addr;
68
Nico Huberb1974022023-01-12 13:13:12 +010069 return 0;
70}
71
72int prepare_memory_register_access(struct flashctx *flash, enum preparation_steps prep)
73{
Nico Huberb89c4522024-12-04 23:30:13 +010074 if (prep == PREPARE_POST_PROBE)
75 return 0;
76
Nico Huberb1974022023-01-12 13:13:12 +010077 if (prepare_memory_access(flash, prep))
78 return 1;
79
Nico Huber0e76d992023-01-12 20:22:55 +010080 /*
81 * FIXME: Special function registers normally live 4 MByte below flash space,
82 * but it might be somewhere completely different on some chips and programmers,
83 * or not mappable at all. Ignore these problems for now and always report success.
84 */
Nico Huberb1974022023-01-12 13:13:12 +010085 const chipsize_t size = flash->chip->total_size * 1024;
86 const uintptr_t base = 0xffffffff - size - 0x400000 + 1;
87 void *const addr = programmer_map_flash_region(flash, "flash chip registers", base, size);
88 if (addr == ERROR_PTR) {
89 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
90 flash->chip->name, PRIxPTR_WIDTH, base);
91 return 0;
Nico Huber0e76d992023-01-12 20:22:55 +010092 }
Nico Huberb1974022023-01-12 13:13:12 +010093 flash->physical_registers = base;
94 flash->virtual_registers = (chipaddr)addr;
95
Nico Huber0e76d992023-01-12 20:22:55 +010096 return 0;
97}
98
99void finish_memory_access(struct flashctx *flash)
100{
101 const size_t size = flashprog_flash_getsize(flash);
102
103 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
104 programmer_unmap_flash_region(flash, (void *)flash->virtual_registers, size);
105 flash->physical_registers = 0;
106 flash->virtual_registers = (chipaddr)ERROR_PTR;
107 }
108
109 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
110 programmer_unmap_flash_region(flash, (void *)flash->virtual_memory, size);
111 flash->physical_memory = 0;
112 flash->virtual_memory = (chipaddr)ERROR_PTR;
113 }
114}