1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Marvell International Ltd.
4 * Copyright (C) 2021 Stefan Roese <sr@denx.de>
14 #include <stdio_dev.h>
17 #include <linux/delay.h>
18 #include <asm/addrspace.h>
20 #include <mach/cvmx-regs.h>
21 #include <mach/cvmx-bootmem.h>
23 #define DRIVER_NAME "pci-bootcmd"
27 * This address cannot be changed as the PCI console tool relies on exactly
30 #define BOOTLOADER_PCI_READ_BUFFER_BASE 0x6c000
31 #define BOOTLOADER_PCI_READ_BUFFER_SIZE 256
32 #define BOOTLOADER_PCI_WRITE_BUFFER_SIZE 256
34 #define BOOTLOADER_PCI_READ_BUFFER_STR_LEN \
35 (BOOTLOADER_PCI_READ_BUFFER_SIZE - 8)
36 #define BOOTLOADER_PCI_WRITE_BUFFER_STR_LEN \
37 (BOOTLOADER_PCI_WRITE_BUFFER_SIZE - 8)
39 #define BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR \
40 (BOOTLOADER_PCI_READ_BUFFER_BASE + 0)
41 #define BOOTLOADER_PCI_READ_BUFFER_LEN_ADDR \
42 (BOOTLOADER_PCI_READ_BUFFER_BASE + 4)
43 #define BOOTLOADER_PCI_READ_BUFFER_DATA_ADDR \
44 (BOOTLOADER_PCI_READ_BUFFER_BASE + 8)
46 enum octeon_pci_io_buf_owner {
47 /* Must be zero, set when memory cleared */
48 OCTEON_PCI_IO_BUF_OWNER_INVALID = 0,
49 OCTEON_PCI_IO_BUF_OWNER_OCTEON = 1,
50 OCTEON_PCI_IO_BUF_OWNER_HOST = 2,
53 /* Structure for bootloader PCI IO buffers */
54 struct octeon_pci_io_buf {
60 struct octeon_bootcmd_priv {
65 struct octeon_pci_io_buf *buf;
68 static int octeon_bootcmd_pending(struct udevice *dev, bool input)
70 struct octeon_bootcmd_priv *priv = dev_get_priv(dev);
79 if (priv->buf->owner != OCTEON_PCI_IO_BUF_OWNER_OCTEON)
82 if (priv->buf->len > priv->copy_offset &&
83 (priv->buf->data[priv->copy_offset] != '\0'))
89 static int octeon_bootcmd_getc(struct udevice *dev)
91 struct octeon_bootcmd_priv *priv = dev_get_priv(dev);
94 /* There's no EOL for boot commands so we fake it. */
100 while (!octeon_bootcmd_pending(dev, true)) {
104 * The original code calls octeon_board_poll() here. We may
105 * need to implement something similar here.
110 c = priv->buf->data[priv->copy_offset];
111 priv->buf->data[priv->copy_offset++] = '\0';
113 if (priv->copy_offset >= min_t(int, CONFIG_SYS_CBSIZE - 1,
114 BOOTLOADER_PCI_READ_BUFFER_STR_LEN - 1) ||
115 (priv->buf->data[priv->copy_offset] == '\0')) {
116 priv->copy_offset = 0;
118 priv->buf->owner = OCTEON_PCI_IO_BUF_OWNER_HOST;
126 static const struct dm_serial_ops octeon_bootcmd_ops = {
127 .getc = octeon_bootcmd_getc,
128 .pending = octeon_bootcmd_pending,
131 static int octeon_bootcmd_probe(struct udevice *dev)
133 struct octeon_bootcmd_priv *priv = dev_get_priv(dev);
135 priv->buf = (void *)CKSEG0ADDR(BOOTLOADER_PCI_READ_BUFFER_BASE);
136 memset(priv->buf, 0, BOOTLOADER_PCI_READ_BUFFER_SIZE);
140 * When the bootcmd console is first started it is started as locked to
141 * block any calls sending a command until U-Boot is ready to accept
142 * commands. Just before the main loop starts to accept commands the
143 * bootcmd console is unlocked.
146 priv->buf->owner = OCTEON_PCI_IO_BUF_OWNER_HOST;
148 priv->buf->owner = OCTEON_PCI_IO_BUF_OWNER_OCTEON;
150 debug("%s called, buffer ptr: 0x%p, owner: %s\n", __func__,
152 priv->buf->owner == OCTEON_PCI_IO_BUF_OWNER_HOST ?
154 debug("&priv->copy_offset: 0x%p\n", &priv->copy_offset);
158 * Perhaps reinvestige this: In the original code, "unlocked" etc
159 * is set in the octeon_pci_bootcmd_unlock() function called very
162 priv->buf->owner = OCTEON_PCI_IO_BUF_OWNER_HOST;
163 priv->unlocked = true;
164 priv->started = true;
170 static const struct udevice_id octeon_bootcmd_serial_id[] = {
171 { .compatible = "marvell,pci-bootcmd", },
175 U_BOOT_DRIVER(octeon_bootcmd) = {
178 .ops = &octeon_bootcmd_ops,
179 .of_match = of_match_ptr(octeon_bootcmd_serial_id),
180 .probe = octeon_bootcmd_probe,
181 .priv_auto = sizeof(struct octeon_bootcmd_priv),