pci: Use common functions to read/write config
Currently we use switch() and access PCI configuration via several functions, one for each data size. Adjust the code to use generic functions, where the data size is a parameter. Signed-off-by: Simon Glass <sjg@chromium.org> Reviewed-by: Bin Meng <bmeng.cn@gmail.com> Tested-by: Bin Meng <bmeng.cn@gmail.com>
This commit is contained in:
parent
c2be070072
commit
72ef5b608c
|
@ -28,19 +28,24 @@ struct pci_reg_info {
|
||||||
u8 offset;
|
u8 offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int pci_field_width(enum pci_size_t size)
|
static int pci_byte_size(enum pci_size_t size)
|
||||||
{
|
{
|
||||||
switch (size) {
|
switch (size) {
|
||||||
case PCI_SIZE_8:
|
case PCI_SIZE_8:
|
||||||
return 2;
|
return 1;
|
||||||
case PCI_SIZE_16:
|
case PCI_SIZE_16:
|
||||||
return 4;
|
return 2;
|
||||||
case PCI_SIZE_32:
|
case PCI_SIZE_32:
|
||||||
default:
|
default:
|
||||||
return 8;
|
return 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int pci_field_width(enum pci_size_t size)
|
||||||
|
{
|
||||||
|
return pci_byte_size(size) * 2;
|
||||||
|
}
|
||||||
|
|
||||||
static unsigned long pci_read_config(pci_dev_t dev, int offset,
|
static unsigned long pci_read_config(pci_dev_t dev, int offset,
|
||||||
enum pci_size_t size)
|
enum pci_size_t size)
|
||||||
{
|
{
|
||||||
|
@ -325,38 +330,31 @@ static pci_dev_t get_pci_dev(char *name)
|
||||||
return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]);
|
return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pci_cfg_display(pci_dev_t bdf, ulong addr, ulong size, ulong length)
|
static int pci_cfg_display(pci_dev_t bdf, ulong addr, enum pci_size_t size,
|
||||||
|
ulong length)
|
||||||
{
|
{
|
||||||
#define DISP_LINE_LEN 16
|
#define DISP_LINE_LEN 16
|
||||||
ulong i, nbytes, linebytes;
|
ulong i, nbytes, linebytes;
|
||||||
|
int byte_size;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
|
byte_size = pci_byte_size(size);
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
length = 0x40 / size; /* Standard PCI configuration space */
|
length = 0x40 / byte_size; /* Standard PCI config space */
|
||||||
|
|
||||||
/* Print the lines.
|
/* Print the lines.
|
||||||
* once, and all accesses are with the specified bus width.
|
* once, and all accesses are with the specified bus width.
|
||||||
*/
|
*/
|
||||||
nbytes = length * size;
|
nbytes = length * byte_size;
|
||||||
do {
|
do {
|
||||||
uint val4;
|
|
||||||
ushort val2;
|
|
||||||
u_char val1;
|
|
||||||
|
|
||||||
printf("%08lx:", addr);
|
printf("%08lx:", addr);
|
||||||
linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
|
linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
|
||||||
for (i=0; i<linebytes; i+= size) {
|
for (i = 0; i < linebytes; i += byte_size) {
|
||||||
if (size == 4) {
|
unsigned long val;
|
||||||
pci_read_config_dword(bdf, addr, &val4);
|
|
||||||
printf(" %08x", val4);
|
val = pci_read_config(bdf, addr, size);
|
||||||
} else if (size == 2) {
|
printf(" %0*lx", pci_field_width(size), val);
|
||||||
pci_read_config_word(bdf, addr, &val2);
|
addr += byte_size;
|
||||||
printf(" %04x", val2);
|
|
||||||
} else {
|
|
||||||
pci_read_config_byte(bdf, addr, &val1);
|
|
||||||
printf(" %02x", val1);
|
|
||||||
}
|
|
||||||
addr += size;
|
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
nbytes -= linebytes;
|
nbytes -= linebytes;
|
||||||
|
@ -385,32 +383,20 @@ static int pci_cfg_write (pci_dev_t bdf, ulong addr, ulong size, ulong value)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int pci_cfg_modify(pci_dev_t bdf, ulong addr, enum pci_size_t size,
|
||||||
pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag)
|
ulong value, int incrflag)
|
||||||
{
|
{
|
||||||
ulong i;
|
ulong i;
|
||||||
int nbytes;
|
int nbytes;
|
||||||
uint val4;
|
ulong val;
|
||||||
ushort val2;
|
|
||||||
u_char val1;
|
|
||||||
|
|
||||||
/* Print the address, followed by value. Then accept input for
|
/* Print the address, followed by value. Then accept input for
|
||||||
* the next value. A non-converted value exits.
|
* the next value. A non-converted value exits.
|
||||||
*/
|
*/
|
||||||
do {
|
do {
|
||||||
printf("%08lx:", addr);
|
printf("%08lx:", addr);
|
||||||
if (size == 4) {
|
val = pci_read_config(bdf, addr, size);
|
||||||
pci_read_config_dword(bdf, addr, &val4);
|
printf(" %0*lx", pci_field_width(size), val);
|
||||||
printf(" %08x", val4);
|
|
||||||
}
|
|
||||||
else if (size == 2) {
|
|
||||||
pci_read_config_word(bdf, addr, &val2);
|
|
||||||
printf(" %04x", val2);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
pci_read_config_byte(bdf, addr, &val1);
|
|
||||||
printf(" %02x", val1);
|
|
||||||
}
|
|
||||||
|
|
||||||
nbytes = cli_readline(" ? ");
|
nbytes = cli_readline(" ? ");
|
||||||
if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
|
if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
|
||||||
|
@ -456,7 +442,8 @@ pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag
|
||||||
*/
|
*/
|
||||||
static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
|
static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
|
||||||
{
|
{
|
||||||
ulong addr = 0, value = 0, size = 0;
|
ulong addr = 0, value = 0, cmd_size = 0;
|
||||||
|
enum pci_size_t size = PCI_SIZE_32;
|
||||||
int busnum = 0;
|
int busnum = 0;
|
||||||
pci_dev_t bdf = 0;
|
pci_dev_t bdf = 0;
|
||||||
char cmd = 's';
|
char cmd = 's';
|
||||||
|
@ -471,7 +458,8 @@ static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
|
||||||
case 'm': /* modify */
|
case 'm': /* modify */
|
||||||
case 'w': /* write */
|
case 'w': /* write */
|
||||||
/* Check for a size specification. */
|
/* Check for a size specification. */
|
||||||
size = cmd_get_data_size(argv[1], 4);
|
cmd_size = cmd_get_data_size(argv[1], 4);
|
||||||
|
size = (cmd_size == 4) ? PCI_SIZE_32 : cmd_size - 1;
|
||||||
if (argc > 3)
|
if (argc > 3)
|
||||||
addr = simple_strtoul(argv[3], NULL, 16);
|
addr = simple_strtoul(argv[3], NULL, 16);
|
||||||
if (argc > 4)
|
if (argc > 4)
|
||||||
|
|
Loading…
Reference in New Issue