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>
19 #include <mach/cvmx-regs.h>
20 #include <mach/cvmx-bootmem.h>
22 #define DRIVER_NAME "pci-console"
23 #define OCTEONTX_PCIE_CONSOLE_NAME_LEN 16
25 /* Current versions */
26 #define OCTEON_PCIE_CONSOLE_MAJOR_VERSION 1
27 #define OCTEON_PCIE_CONSOLE_MINOR_VERSION 0
29 #define OCTEON_PCIE_CONSOLE_BLOCK_NAME "__pci_console"
32 * Structure that defines a single console.
33 * Note: when read_index == write_index, the buffer is empty.
34 * The actual usable size of each console is console_buf_size -1;
36 struct octeon_pcie_console {
39 u32 input_write_index;
41 u32 output_read_index;
42 u32 output_write_index;
48 * This is the main container structure that contains all the information
49 * about all PCI consoles. The address of this structure is passed to various
50 * routines that operation on PCI consoles.
52 struct octeon_pcie_console_desc {
59 /* must be 64 bit aligned here... */
60 /* Array of addresses of octeon_pcie_console_t structures */
61 u64 console_addr_array[0];
62 /* Implicit storage for console_addr_array */
65 struct octeon_pcie_console_priv {
66 struct octeon_pcie_console *console;
71 /* Flag definitions for read/write functions */
74 * If set, read/write functions won't block waiting for space or data.
75 * For reads, 0 bytes may be read, and for writes not all of the
76 * supplied data may be written.
78 OCT_PCI_CON_FLAG_NONBLOCK = 1 << 0,
81 static int buffer_free_bytes(u32 buffer_size, u32 wr_idx, u32 rd_idx)
83 if (rd_idx >= buffer_size || wr_idx >= buffer_size)
86 return ((buffer_size - 1) - (wr_idx - rd_idx)) % buffer_size;
89 static int buffer_avail_bytes(u32 buffer_size, u32 wr_idx, u32 rd_idx)
91 if (rd_idx >= buffer_size || wr_idx >= buffer_size)
94 return buffer_size - 1 - buffer_free_bytes(buffer_size, wr_idx, rd_idx);
97 static int buffer_read_avail(struct udevice *dev, unsigned int console_num)
99 struct octeon_pcie_console_priv *priv = dev_get_priv(dev);
100 struct octeon_pcie_console *cons_ptr = priv->console;
103 avail = buffer_avail_bytes(cons_ptr->buf_size,
104 cons_ptr->input_write_index,
105 cons_ptr->input_read_index);
112 static int octeon_pcie_console_read(struct udevice *dev,
113 unsigned int console_num, char *buffer,
114 int buffer_size, u32 flags)
116 struct octeon_pcie_console_priv *priv = dev_get_priv(dev);
117 struct octeon_pcie_console *cons_ptr = priv->console;
123 buf_ptr = (char *)cvmx_phys_to_ptr(cons_ptr->input_base_addr);
125 avail = buffer_avail_bytes(cons_ptr->buf_size,
126 cons_ptr->input_write_index,
127 cons_ptr->input_read_index);
131 if (!(flags & OCT_PCI_CON_FLAG_NONBLOCK)) {
132 /* Wait for some data to be available */
133 while (0 == (avail = buffer_avail_bytes(cons_ptr->buf_size,
134 cons_ptr->input_write_index,
135 cons_ptr->input_read_index))) {
143 /* Don't overflow the buffer passed to us */
144 read_size = min_t(int, avail, buffer_size);
146 /* Limit ourselves to what we can input in a contiguous block */
147 if (cons_ptr->input_read_index + read_size >= cons_ptr->buf_size)
148 read_size = cons_ptr->buf_size - cons_ptr->input_read_index;
150 memcpy(buffer, buf_ptr + cons_ptr->input_read_index, read_size);
151 cons_ptr->input_read_index =
152 (cons_ptr->input_read_index + read_size) % cons_ptr->buf_size;
153 bytes_read += read_size;
155 /* Mark the PCIe console to be active from now on */
157 priv->console_active = true;
162 static int octeon_pcie_console_write(struct udevice *dev,
163 unsigned int console_num,
165 int bytes_to_write, u32 flags)
167 struct octeon_pcie_console_priv *priv = dev_get_priv(dev);
168 struct octeon_pcie_console *cons_ptr = priv->console;
173 buf_ptr = (char *)cvmx_phys_to_ptr(cons_ptr->output_base_addr);
175 while (bytes_to_write > 0) {
176 avail = buffer_free_bytes(cons_ptr->buf_size,
177 cons_ptr->output_write_index,
178 cons_ptr->output_read_index);
181 int write_size = min_t(int, avail, bytes_to_write);
184 * Limit ourselves to what we can output in a contiguous
187 if (cons_ptr->output_write_index + write_size >=
188 cons_ptr->buf_size) {
189 write_size = cons_ptr->buf_size -
190 cons_ptr->output_write_index;
193 memcpy(buf_ptr + cons_ptr->output_write_index,
194 buffer + bytes_written, write_size);
196 * Make sure data is visible before changing write
200 cons_ptr->output_write_index =
201 (cons_ptr->output_write_index + write_size) %
203 bytes_to_write -= write_size;
204 bytes_written += write_size;
205 } else if (avail == 0) {
207 * Check to see if we should wait for room, or return
208 * after a partial write
210 if (flags & OCT_PCI_CON_FLAG_NONBLOCK)
214 mdelay(10); /* Delay if we are spinning */
222 return bytes_written;
225 static struct octeon_pcie_console_desc *octeon_pcie_console_init(int num_consoles,
228 struct octeon_pcie_console_desc *cons_desc_ptr;
229 struct octeon_pcie_console *cons_ptr;
235 /* Compute size required for pci console structure */
236 alloc_size = num_consoles *
237 (buffer_size * 2 + sizeof(struct octeon_pcie_console) +
238 sizeof(u64)) + sizeof(struct octeon_pcie_console_desc);
241 * Allocate memory for the consoles. This must be in the range
242 * addresssible by the bootloader.
243 * Try to do so in a manner which minimizes fragmentation. We try to
244 * put it at the top of DDR0 or bottom of DDR2 first, and only do
245 * generic allocation if those fail
247 addr = cvmx_bootmem_phy_named_block_alloc(alloc_size,
248 OCTEON_DDR0_SIZE - alloc_size - 128,
249 OCTEON_DDR0_SIZE, 128,
250 OCTEON_PCIE_CONSOLE_BLOCK_NAME,
251 CVMX_BOOTMEM_FLAG_END_ALLOC);
253 addr = cvmx_bootmem_phy_named_block_alloc(alloc_size, 0,
255 OCTEON_PCIE_CONSOLE_BLOCK_NAME,
256 CVMX_BOOTMEM_FLAG_END_ALLOC);
261 cons_desc_ptr = cvmx_phys_to_ptr(addr);
263 /* Clear entire alloc'ed memory */
264 memset(cons_desc_ptr, 0, alloc_size);
266 /* Initialize as locked until we are done */
267 cons_desc_ptr->lock = 1;
269 cons_desc_ptr->num_consoles = num_consoles;
270 cons_desc_ptr->flags = 0;
271 cons_desc_ptr->major_version = OCTEON_PCIE_CONSOLE_MAJOR_VERSION;
272 cons_desc_ptr->minor_version = OCTEON_PCIE_CONSOLE_MINOR_VERSION;
274 avail_addr = addr + sizeof(struct octeon_pcie_console_desc) +
275 num_consoles * sizeof(u64);
277 for (i = 0; i < num_consoles; i++) {
278 cons_desc_ptr->console_addr_array[i] = avail_addr;
279 cons_ptr = (void *)cons_desc_ptr->console_addr_array[i];
280 avail_addr += sizeof(struct octeon_pcie_console);
281 cons_ptr->input_base_addr = avail_addr;
282 avail_addr += buffer_size;
283 cons_ptr->output_base_addr = avail_addr;
284 avail_addr += buffer_size;
285 cons_ptr->buf_size = buffer_size;
288 cons_desc_ptr->lock = 0;
290 return cvmx_phys_to_ptr(addr);
293 static int octeon_pcie_console_getc(struct udevice *dev)
297 octeon_pcie_console_read(dev, 0, &c, 1, 0);
301 static int octeon_pcie_console_putc(struct udevice *dev, const char c)
303 struct octeon_pcie_console_priv *priv = dev_get_priv(dev);
305 if (priv->console_active)
306 octeon_pcie_console_write(dev, 0, (char *)&c, 1, 0);
311 static int octeon_pcie_console_pending(struct udevice *dev, bool input)
315 return buffer_read_avail(dev, 0) > 0;
321 static const struct dm_serial_ops octeon_pcie_console_ops = {
322 .getc = octeon_pcie_console_getc,
323 .putc = octeon_pcie_console_putc,
324 .pending = octeon_pcie_console_pending,
327 static int octeon_pcie_console_probe(struct udevice *dev)
329 struct octeon_pcie_console_priv *priv = dev_get_priv(dev);
330 struct octeon_pcie_console_desc *cons_desc;
336 * Currently only 1 console is supported. Perhaps we need to add
337 * a console nexus if more than one needs to be supported.
343 cons_desc = octeon_pcie_console_init(console_count, console_size);
345 cvmx_phys_to_ptr(cons_desc->console_addr_array[console_num]);
347 debug("PCI console init succeeded, %d consoles, %d bytes each\n",
348 console_count, console_size);
353 static const struct udevice_id octeon_pcie_console_serial_id[] = {
354 { .compatible = "marvell,pci-console", },
358 U_BOOT_DRIVER(octeon_pcie_console) = {
361 .ops = &octeon_pcie_console_ops,
362 .of_match = of_match_ptr(octeon_pcie_console_serial_id),
363 .probe = octeon_pcie_console_probe,
364 .priv_auto = sizeof(struct octeon_pcie_console_priv),