summaryrefslogtreecommitdiff
path: root/drivers/serial/serial_octeon_pcie_console.c
blob: b0eafe7ad8667b6c76ae600c3759556a2a2f1a01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2019 Marvell International Ltd.
 * Copyright (C) 2021 Stefan Roese <sr@denx.de>
 */

#include <dm.h>
#include <dm/uclass.h>
#include <errno.h>
#include <input.h>
#include <iomux.h>
#include <log.h>
#include <serial.h>
#include <stdio_dev.h>
#include <string.h>
#include <watchdog.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <mach/cvmx-regs.h>
#include <mach/cvmx-bootmem.h>

#define DRIVER_NAME				"pci-console"
#define OCTEONTX_PCIE_CONSOLE_NAME_LEN		16

/* Current versions */
#define OCTEON_PCIE_CONSOLE_MAJOR_VERSION	1
#define OCTEON_PCIE_CONSOLE_MINOR_VERSION	0

#define OCTEON_PCIE_CONSOLE_BLOCK_NAME		"__pci_console"

/*
 * Structure that defines a single console.
 * Note: when read_index == write_index, the buffer is empty.
 * The actual usable size of each console is console_buf_size -1;
 */
struct octeon_pcie_console {
	u64 input_base_addr;
	u32 input_read_index;
	u32 input_write_index;
	u64 output_base_addr;
	u32 output_read_index;
	u32 output_write_index;
	u32 lock;
	u32 buf_size;
};

/*
 * This is the main container structure that contains all the information
 * about all PCI consoles. The address of this structure is passed to various
 * routines that operation on PCI consoles.
 */
struct octeon_pcie_console_desc {
	u32 major_version;
	u32 minor_version;
	u32 lock;
	u32 flags;
	u32 num_consoles;
	u32 pad;
	/* must be 64 bit aligned here... */
	/* Array of addresses of octeon_pcie_console_t structures */
	u64 console_addr_array[0];
	/* Implicit storage for console_addr_array */
};

struct octeon_pcie_console_priv {
	struct octeon_pcie_console *console;
	int console_num;
	bool console_active;
};

/* Flag definitions for read/write functions */
enum {
	/*
	 * If set, read/write functions won't block waiting for space or data.
	 * For reads, 0 bytes may be read, and for writes not all of the
	 * supplied data may be written.
	 */
	OCT_PCI_CON_FLAG_NONBLOCK = 1 << 0,
};

static int buffer_free_bytes(u32 buffer_size, u32 wr_idx, u32 rd_idx)
{
	if (rd_idx >= buffer_size || wr_idx >= buffer_size)
		return -1;

	return ((buffer_size - 1) - (wr_idx - rd_idx)) % buffer_size;
}

static int buffer_avail_bytes(u32 buffer_size, u32 wr_idx, u32 rd_idx)
{
	if (rd_idx >= buffer_size || wr_idx >= buffer_size)
		return -1;

	return buffer_size - 1 - buffer_free_bytes(buffer_size, wr_idx, rd_idx);
}

static int buffer_read_avail(struct udevice *dev, unsigned int console_num)
{
	struct octeon_pcie_console_priv *priv = dev_get_priv(dev);
	struct octeon_pcie_console *cons_ptr = priv->console;
	int avail;

	avail = buffer_avail_bytes(cons_ptr->buf_size,
				   cons_ptr->input_write_index,
				   cons_ptr->input_read_index);
	if (avail >= 0)
		return avail;

	return 0;
}

static int octeon_pcie_console_read(struct udevice *dev,
				    unsigned int console_num, char *buffer,
				    int buffer_size, u32 flags)
{
	struct octeon_pcie_console_priv *priv = dev_get_priv(dev);
	struct octeon_pcie_console *cons_ptr = priv->console;
	int avail;
	char *buf_ptr;
	int bytes_read;
	int read_size;

	buf_ptr = (char *)cvmx_phys_to_ptr(cons_ptr->input_base_addr);

	avail =	buffer_avail_bytes(cons_ptr->buf_size,
				   cons_ptr->input_write_index,
				   cons_ptr->input_read_index);
	if (avail < 0)
		return avail;

	if (!(flags & OCT_PCI_CON_FLAG_NONBLOCK)) {
		/* Wait for some data to be available */
		while (0 == (avail = buffer_avail_bytes(cons_ptr->buf_size,
							cons_ptr->input_write_index,
							cons_ptr->input_read_index))) {
			mdelay(10);
			schedule();
		}
	}

	bytes_read = 0;

	/* Don't overflow the buffer passed to us */
	read_size = min_t(int, avail, buffer_size);

	/* Limit ourselves to what we can input in a contiguous block */
	if (cons_ptr->input_read_index + read_size >= cons_ptr->buf_size)
		read_size = cons_ptr->buf_size - cons_ptr->input_read_index;

	memcpy(buffer, buf_ptr + cons_ptr->input_read_index, read_size);
	cons_ptr->input_read_index =
		(cons_ptr->input_read_index + read_size) % cons_ptr->buf_size;
	bytes_read += read_size;

	/* Mark the PCIe console to be active from now on */
	if (bytes_read)
		priv->console_active = true;

	return bytes_read;
}

static int octeon_pcie_console_write(struct udevice *dev,
				     unsigned int console_num,
				     const char *buffer,
				     int bytes_to_write, u32 flags)
{
	struct octeon_pcie_console_priv *priv = dev_get_priv(dev);
	struct octeon_pcie_console *cons_ptr = priv->console;
	int avail;
	char *buf_ptr;
	int bytes_written;

	buf_ptr = (char *)cvmx_phys_to_ptr(cons_ptr->output_base_addr);
	bytes_written = 0;
	while (bytes_to_write > 0) {
		avail = buffer_free_bytes(cons_ptr->buf_size,
					  cons_ptr->output_write_index,
					  cons_ptr->output_read_index);

		if (avail > 0) {
			int write_size = min_t(int, avail, bytes_to_write);

			/*
			 * Limit ourselves to what we can output in a contiguous
			 * block
			 */
			if (cons_ptr->output_write_index + write_size >=
			    cons_ptr->buf_size) {
				write_size = cons_ptr->buf_size -
					     cons_ptr->output_write_index;
			}

			memcpy(buf_ptr + cons_ptr->output_write_index,
			       buffer + bytes_written, write_size);
			/*
			 * Make sure data is visible before changing write
			 * index
			 */
			CVMX_SYNCW;
			cons_ptr->output_write_index =
				(cons_ptr->output_write_index + write_size) %
				cons_ptr->buf_size;
			bytes_to_write -= write_size;
			bytes_written += write_size;
		} else if (avail == 0) {
			/*
			 * Check to see if we should wait for room, or return
			 * after a partial write
			 */
			if (flags & OCT_PCI_CON_FLAG_NONBLOCK)
				goto done;

			schedule();
			mdelay(10);	/* Delay if we are spinning */
		} else {
			bytes_written = -1;
			goto done;
		}
	}

done:
	return bytes_written;
}

static struct octeon_pcie_console_desc *octeon_pcie_console_init(int num_consoles,
								 int buffer_size)
{
	struct octeon_pcie_console_desc *cons_desc_ptr;
	struct octeon_pcie_console *cons_ptr;
	s64 addr;
	u64 avail_addr;
	int alloc_size;
	int i;

	/* Compute size required for pci console structure */
	alloc_size = num_consoles *
		(buffer_size * 2 + sizeof(struct octeon_pcie_console) +
		 sizeof(u64)) + sizeof(struct octeon_pcie_console_desc);

	/*
	 * Allocate memory for the consoles.  This must be in the range
	 * addresssible by the bootloader.
	 * Try to do so in a manner which minimizes fragmentation.  We try to
	 * put it at the top of DDR0 or bottom of DDR2 first, and only do
	 * generic allocation if those fail
	 */
	addr = cvmx_bootmem_phy_named_block_alloc(alloc_size,
						  OCTEON_DDR0_SIZE - alloc_size - 128,
						  OCTEON_DDR0_SIZE, 128,
						  OCTEON_PCIE_CONSOLE_BLOCK_NAME,
						  CVMX_BOOTMEM_FLAG_END_ALLOC);
	if (addr < 0) {
		addr = cvmx_bootmem_phy_named_block_alloc(alloc_size, 0,
							  0x1fffffff, 128,
							  OCTEON_PCIE_CONSOLE_BLOCK_NAME,
							  CVMX_BOOTMEM_FLAG_END_ALLOC);
	}
	if (addr < 0)
		return 0;

	cons_desc_ptr = cvmx_phys_to_ptr(addr);

	/* Clear entire alloc'ed memory */
	memset(cons_desc_ptr, 0, alloc_size);

	/* Initialize as locked until we are done */
	cons_desc_ptr->lock = 1;
	CVMX_SYNCW;
	cons_desc_ptr->num_consoles = num_consoles;
	cons_desc_ptr->flags = 0;
	cons_desc_ptr->major_version = OCTEON_PCIE_CONSOLE_MAJOR_VERSION;
	cons_desc_ptr->minor_version = OCTEON_PCIE_CONSOLE_MINOR_VERSION;

	avail_addr = addr + sizeof(struct octeon_pcie_console_desc) +
		num_consoles * sizeof(u64);

	for (i = 0; i < num_consoles; i++) {
		cons_desc_ptr->console_addr_array[i] = avail_addr;
		cons_ptr = (void *)cons_desc_ptr->console_addr_array[i];
		avail_addr += sizeof(struct octeon_pcie_console);
		cons_ptr->input_base_addr = avail_addr;
		avail_addr += buffer_size;
		cons_ptr->output_base_addr = avail_addr;
		avail_addr += buffer_size;
		cons_ptr->buf_size = buffer_size;
	}
	CVMX_SYNCW;
	cons_desc_ptr->lock = 0;

	return cvmx_phys_to_ptr(addr);
}

static int octeon_pcie_console_getc(struct udevice *dev)
{
	char c;

	octeon_pcie_console_read(dev, 0, &c, 1, 0);
	return c;
}

static int octeon_pcie_console_putc(struct udevice *dev, const char c)
{
	struct octeon_pcie_console_priv *priv = dev_get_priv(dev);

	if (priv->console_active)
		octeon_pcie_console_write(dev, 0, (char *)&c, 1, 0);

	return 0;
}

static int octeon_pcie_console_pending(struct udevice *dev, bool input)
{
	if (input) {
		udelay(100);
		return buffer_read_avail(dev, 0) > 0;
	}

	return 0;
}

static const struct dm_serial_ops octeon_pcie_console_ops = {
	.getc = octeon_pcie_console_getc,
	.putc = octeon_pcie_console_putc,
	.pending = octeon_pcie_console_pending,
};

static int octeon_pcie_console_probe(struct udevice *dev)
{
	struct octeon_pcie_console_priv *priv = dev_get_priv(dev);
	struct octeon_pcie_console_desc *cons_desc;
	int console_count;
	int console_size;
	int console_num;

	/*
	 * Currently only 1 console is supported. Perhaps we need to add
	 * a console nexus if more than one needs to be supported.
	 */
	console_count = 1;
	console_size = 1024;
	console_num = 0;

	cons_desc = octeon_pcie_console_init(console_count, console_size);
	priv->console =
		cvmx_phys_to_ptr(cons_desc->console_addr_array[console_num]);

	debug("PCI console init succeeded, %d consoles, %d bytes each\n",
	      console_count, console_size);

	return 0;
}

static const struct udevice_id octeon_pcie_console_serial_id[] = {
	{ .compatible = "marvell,pci-console", },
	{ },
};

U_BOOT_DRIVER(octeon_pcie_console) = {
	.name = DRIVER_NAME,
	.id = UCLASS_SERIAL,
	.ops = &octeon_pcie_console_ops,
	.of_match = of_match_ptr(octeon_pcie_console_serial_id),
	.probe = octeon_pcie_console_probe,
	.priv_auto = sizeof(struct octeon_pcie_console_priv),
};