blob: 42ea2e3b911f1cff0171ebc8fe2a878ecfd852cb [file] [log] [blame]
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00004 * Copyright (C) 2009,2010,2011 Carl-Daniel Hailfinger
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +00005 *
6 * 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.
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000015 */
16
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000017#include "flash.h"
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000018#include "programmer.h"
19
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000020/* Fallback map() for programmers which don't need special handling */
Stefan Tauner305e0b92013-07-17 23:46:44 +000021void *fallback_map(const char *descr, uintptr_t phys_addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000022{
23 /* FIXME: Should return phys_addr. */
Peter Huewe73f8ec82011-01-24 19:15:51 +000024 return NULL;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000025}
26
27/* No-op/fallback unmap() for programmers which don't need special handling */
28void fallback_unmap(void *virt_addr, size_t len)
29{
30}
31
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000032/* Little-endian fallback for drivers not supporting 16 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000033void fallback_chip_writew(const struct flashctx *flash, uint16_t val,
34 chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000035{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000036 chip_writeb(flash, val & 0xff, addr);
37 chip_writeb(flash, (val >> 8) & 0xff, addr + 1);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000038}
39
40/* Little-endian fallback for drivers not supporting 16 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000041uint16_t fallback_chip_readw(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000042{
43 uint16_t val;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000044 val = chip_readb(flash, addr);
45 val |= chip_readb(flash, addr + 1) << 8;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000046 return val;
47}
48
49/* Little-endian fallback for drivers not supporting 32 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000050void fallback_chip_writel(const struct flashctx *flash, uint32_t val,
51 chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000052{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000053 chip_writew(flash, val & 0xffff, addr);
54 chip_writew(flash, (val >> 16) & 0xffff, addr + 2);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000055}
56
57/* Little-endian fallback for drivers not supporting 32 bit accesses */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000058uint32_t fallback_chip_readl(const struct flashctx *flash, const chipaddr addr)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000059{
60 uint32_t val;
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000061 val = chip_readw(flash, addr);
62 val |= chip_readw(flash, addr + 2) << 16;
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000063 return val;
64}
65
Mark Marshallf20b7be2014-05-09 21:16:21 +000066void fallback_chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000067{
68 size_t i;
69 for (i = 0; i < len; i++)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000070 chip_writeb(flash, buf[i], addr + i);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000071 return;
72}
73
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000074void fallback_chip_readn(const struct flashctx *flash, uint8_t *buf,
75 chipaddr addr, size_t len)
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000076{
77 size_t i;
78 for (i = 0; i < len; i++)
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000079 buf[i] = chip_readb(flash, addr + i);
Carl-Daniel Hailfingercc1802d2010-01-06 10:21:00 +000080 return;
81}
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000082
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000083int register_par_master(const struct par_master *mst,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000084 const enum chipbustype buses)
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +000085{
Edward O'Callaghan4eef6512021-02-03 11:19:41 +110086 struct registered_master rmst = {0};
87
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000088 if (!mst->chip_writeb || !mst->chip_writew || !mst->chip_writel ||
89 !mst->chip_writen || !mst->chip_readb || !mst->chip_readw ||
90 !mst->chip_readl || !mst->chip_readn) {
91 msg_perr("%s called with incomplete master definition. "
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000092 "Please report a bug at flashrom@flashrom.org\n",
93 __func__);
94 return ERROR_FLASHROM_BUG;
95 }
96
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000097 rmst.buses_supported = buses;
98 rmst.par = *mst;
99 return register_master(&rmst);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000100}
101
102/* The limit of 4 is totally arbitrary. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000103#define MASTERS_MAX 4
104struct registered_master registered_masters[MASTERS_MAX];
105int registered_master_count = 0;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000106
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000107/* This function copies the struct registered_master parameter. */
Stefan Tauner5c316f92015-02-08 21:57:52 +0000108int register_master(const struct registered_master *mst)
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000109{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000110 if (registered_master_count >= MASTERS_MAX) {
111 msg_perr("Tried to register more than %i master "
112 "interfaces.\n", MASTERS_MAX);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000113 return ERROR_FLASHROM_LIMIT;
114 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000115 registered_masters[registered_master_count] = *mst;
116 registered_master_count++;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000117
118 return 0;
119}
120
121enum chipbustype get_buses_supported(void)
122{
123 int i;
124 enum chipbustype ret = BUS_NONE;
125
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000126 for (i = 0; i < registered_master_count; i++)
127 ret |= registered_masters[i].buses_supported;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000128
129 return ret;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000130}