aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/soc/marvell/octeontx2-pcicons/otx2-pci-console.c
blob: a13ffd32c73a57ce9af958b2ac32df34e5f69231 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2021 Marvell International Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Supports the PCI Console when OcteonTX2 is running as an Endpoint.
 *
 */

/* Implementation notes:
 *
 * There are four types of devices for which a driver is provided by this
 * module:
 *
 * - PCI console nexus device
 * - PCI console device
 * - Linux console
 * - Linux TTY
 *
 * The primary device to which the driver initially attaches is the
 * 'PCI console nexus', represented in the Device Tree as
 * 'pci-console-nexus@0x7f000000'.
 *
 * The driver entry points are declared in 'pci_console_nexus_driver'.
 *
 * During its initialization, the pci_console_nexus_driver locates its device
 * memory and verifies that it has been configured appropriately
 * (i.e. by U-Boot).
 *
 * Next, it registers a platform driver for the actual console devices;
 * this driver's entry points are declared in 'pci_console_driver'.
 *
 * Finally, it populates the Device Tree with the console devices, which are
 * represented in the Device Tree as 'pci-console@{0-7}' – these are children
 * of the 'PCI console nexus' device.
 *
 * At this point, Linux will probe each new console device, which in turn will
 * register a Linux console.  The entry points for the Linux console are
 * declared as part of the private state structure of the Linux console device
 * (see invocation of 'register_console()' in the function 'pci_console_init()'.
 *
 * The Linux console device will, in turn, register a Linux TTY device.  These
 * device entry points are declared in 'pci_console_dev_tty_ops'.
 *
 * It is the Linux console & TTY devices which actually transfer data between
 * Linux and the OcteonTX device memory; the OcteonTX device memory is accessed
 * by the host remote console application.  This data transfer uses low-level
 * OcteonTX functions.
 *
 * Naming conventions:
 *
 * PCI console nexus device functions are named 'pci_console_nexus_xxx'.
 * PCI console device functions are named 'pci_console_xxx'.
 * Linux console device functions are named 'pci_console_dev_xxx'.
 * Linux TTY device functions are named 'pci_console_dev_tty_xxx'.
 * Low-level OcteonTX routines are named 'octeontx_console_xxx'.
 *
 */

#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/uaccess.h>
#include <linux/uio_driver.h>
#include <linux/irqchip/arm-gic-v3.h>
#include <linux/dma-mapping.h>
#include <linux/device.h>
#include <linux/iommu.h>
#include <linux/of_address.h>
#include <linux/console.h>
#include <linux/delay.h>
#include <linux/tty_driver.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include "otx2-pci-console.h"

#define DRV_NAME       "pci-console"
#define NEXUS_DRV_NAME DRV_NAME "-nexus"

/* copied from Octeon pci console driver */
#define TTY_DRV_MAJOR_VER       4
#define TTY_DRV_MINOR_VER_START 96

#ifdef CONFIG_OCTEONTX2_PCI_CONSOLE_DEBUG
#  define dbgmsg(dev, ...) dev_info((dev), __VA_ARGS__)
#else
#  define dbgmsg(dev, ...) (void)(dev)
#endif // CONFIG_OCTEONTX2_PCI_CONSOLE_DEBUG

static u32 max_consoles = 1;
module_param(max_consoles, uint, 0644);
MODULE_PARM_DESC(max_consoles, "Maximum console count to support");

/* pci console driver prototypes */
static void pci_console_dev_write(struct console *cons, const char *buf,
				  unsigned int len);
static struct tty_driver *pci_console_dev_device(struct console *cons,
						 int *index);
static int pci_console_dev_setup(struct console *cons, char *arg);
static struct platform_driver pci_console_driver;

/* pci console TTY driver prototypes */
static int pci_console_dev_tty_open(struct tty_struct *tty, struct file *filp);
static void pci_console_dev_tty_close(struct tty_struct *tty,
				      struct file *filp);
static int pci_console_dev_tty_write(struct tty_struct *tty,
				     const unsigned char *buf, int count);
static int pci_console_dev_tty_write_room(struct tty_struct *tty);
static int pci_console_dev_tty_chars_in_buffer(struct tty_struct *tty);
static void pci_console_dev_tty_send_xchar(struct tty_struct *tty, char ch);

/* TTY driver operations table */
static const struct tty_operations pci_console_dev_tty_ops = {
	.open = pci_console_dev_tty_open,
	.close = pci_console_dev_tty_close,
	.write = pci_console_dev_tty_write,
	.write_room = pci_console_dev_tty_write_room,
	.chars_in_buffer = pci_console_dev_tty_chars_in_buffer,
	.send_xchar = pci_console_dev_tty_send_xchar,
};

static u32 max_cons_mask;

/*
 * Utility function; returns the number of free bytes in the buffer.
 *
 * @param buffer_size	size of buffer
 * @param wr_idx	write index
 * @param rd_idx	read index
 *
 * @return number of bytes free
 */
static int buffer_free_bytes(size_t 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;
}

/*
 * Utility function; returns the number of pending bytes (i.e. data) in the
 * buffer.
 *
 * @param buffer_size	size of buffer
 * @param wr_idx	write index
 * @param rd_idx	read index
 *
 * @return number of pending data bytes
 */
static int buffer_pending_bytes(size_t 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);
}

/* ======================== pci console nexus driver ======================== */

/*
 * Check that the console version is acceptable.
 */
static bool pci_console_nexus_check_ver(u8 major, u8 minor)
{
	if (major > OCTEONTX_PCIE_CONSOLE_MAJOR)
		return true;
	if (major == OCTEONTX_PCIE_CONSOLE_MAJOR &&
	    minor >= OCTEONTX_PCIE_CONSOLE_MINOR)
		return true;
	return false;
}

/*
 * Used to initialize access to nexus memory.
 */
static int pci_console_nexus_init_resources(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pci_console_nexus *pci_cons_nexus = platform_get_drvdata(pdev);
	struct device_node *of_node;
	const __be32 *of_base;
	u64 of_xbase, of_size;
	int ret;

	dbgmsg(dev, "%s: entry\n", __func__);

	WARN_ON(!pci_cons_nexus);

	ret = -ENODEV;

	pci_cons_nexus->of_node = of_node = pdev->dev.of_node;
	if (!of_node) {
		dev_err(dev, "Missing devicetree configuration\n");
		goto exit;
	}

	of_base = of_get_address(of_node, 0, &of_size, 0);
	if (!of_base) {
		dev_err(dev, "Missing configuration base address\n");
		goto exit;
	}

	of_xbase = of_translate_address(of_node, of_base);
	/* TODO: verify we can use WC */
	if (of_xbase != OF_BAD_ADDR)
		pci_cons_nexus->desc =
			ioremap_wc(of_xbase, of_size);

	if (!pci_cons_nexus->desc) {
		dev_err(dev, "Invalid configuration base address\n");
		goto exit;
	}

	dbgmsg(dev, "of_base: %p (%llx), of_size: %llx, nexus:%p\n",
	       of_base, of_xbase, of_size, pci_cons_nexus->desc);

	ret = 0;

exit:
	return ret ? -ENODEV : 0;
}

static int pci_console_nexus_de_init_resources(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pci_console_nexus *pci_cons_nexus = platform_get_drvdata(pdev);

	dbgmsg(dev, "%s: entry\n", __func__);

	if (pci_cons_nexus && pci_cons_nexus->desc) {
		iounmap(pci_cons_nexus->desc);
		pci_cons_nexus->desc = NULL;
	}

	return 0;
}

/*
 * This is used to initialize the console nexus state.
 */
static int pci_console_nexus_init(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pci_console_nexus *pci_cons_nexus = platform_get_drvdata(pdev);
	struct octeontx_pcie_console_nexus __iomem *nexus;
	struct device_node *child_node;
	uint num_consoles;
	int ret;

	dbgmsg(dev, "%s: entry\n", __func__);

	WARN_ON(!pci_cons_nexus);

	ret = -ENODEV;

	nexus = pci_cons_nexus->desc;
	if (!nexus)
		goto exit;

	/* Verify/use existing configuration (i.e. from U-Boot) */
	if (readq(&nexus->magic) !=
		cpu_to_le64(OCTEONTX_PCIE_CONSOLE_NEXUS_MAGIC)) {
		dev_err(dev, "Invalid nexus signature (0x%llx).\n",
			(long long)readq(&nexus->magic));
		goto exit;
	}

	if (!pci_console_nexus_check_ver(readb(&nexus->major_version),
					 readb(&nexus->minor_version))) {
		dev_err(dev,
			"Unsupported nexus version %u.%u (%u.%u)\n)",
			readb(&nexus->major_version),
			readb(&nexus->minor_version),
			OCTEONTX_PCIE_CONSOLE_MAJOR,
			OCTEONTX_PCIE_CONSOLE_MINOR);
		goto exit;
	}

	if (!readb(&nexus->num_consoles)) {
		dev_err(dev, "No consoles present");
		goto exit;
	}

	/* enumerate 'available' consoles present in device tree */
	num_consoles = 0;
	for_each_available_child_of_node(pci_cons_nexus->of_node,
					 child_node)
		if (of_device_is_compatible(child_node,
					    "marvell,pci-console"))
			num_consoles++;

	if (num_consoles < readb(&nexus->num_consoles)) {
		dev_err(dev,
			"Console count mismatch: DT %d, nexus: %d\n",
			num_consoles, readb(&nexus->num_consoles));
		goto exit;
	}

	dbgmsg(dev,
	       "Console nexus initialized: ver %u.%u, %u consoles available\n",
	       readb(&nexus->major_version), readb(&nexus->minor_version),
	       readb(&nexus->num_consoles));

	ret = 0;

exit:
	return ret ? -ENODEV : 0;
}

/*
 * This is the main probe routine for the console nexus driver.
 */
static int pci_console_nexus_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pci_console_nexus *pci_cons_nexus;
	bool registered;
	int ret;

	BUILD_BUG_ON(offsetof(struct octeontx_pcie_console_nexus, console_addr)
		     != 128);

	dbgmsg(dev, "%s: entry, max_consoles %d\n", __func__, max_consoles);

	max_cons_mask = BIT(max_consoles) - 1;

	pci_cons_nexus = NULL;
	registered = false;

	ret = -ENODEV;

	/* allocate device structure */
	pci_cons_nexus = devm_kzalloc(dev, sizeof(*pci_cons_nexus),
				       GFP_KERNEL);

	if (pci_cons_nexus == NULL) {
		ret = -ENOMEM;
		dev_err(dev, "Unable to allocate drv context.\n");
		goto exit;
	}

	platform_set_drvdata(pdev, pci_cons_nexus);

	ret = pci_console_nexus_init_resources(pdev);
	if (ret)
		goto exit;

	ret = pci_console_nexus_init(pdev);
	if (ret)
		goto exit;

	dev_info(dev, "Registering child console driver...\n");

	ret = platform_driver_register(&pci_console_driver);

	if (ret) {
		dev_err(dev,
			"Error %d registering child console driver\n",
			ret);
		goto exit;
	} else
		registered = true;

	ret = of_platform_populate(pci_cons_nexus->of_node, NULL, NULL,
				   dev);

	if (ret) {
		dev_err(dev, "Error %d populating children of %s\n",
			ret,
			of_node_full_name(pci_cons_nexus->of_node));
		goto exit;
	}

	ret = 0;

exit:
	if (ret) {
		if (registered)
			platform_driver_unregister(&pci_console_driver);

		pci_console_nexus_de_init_resources(pdev);

		if (pci_cons_nexus != NULL)
			devm_kfree(dev, pci_cons_nexus);
	}

	return ret;
}

static void pci_console_nexus_shutdown(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;

	dbgmsg(dev, "%s: entry\n", __func__);
}

/*
 * Linux driver callback.
 */
static int pci_console_nexus_remove(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pci_console_nexus *pci_cons_nexus = platform_get_drvdata(pdev);

	dbgmsg(dev, "%s: entry\n", __func__);

	WARN_ON(!pci_cons_nexus);

	of_platform_depopulate(dev);

	platform_driver_unregister(&pci_console_driver);

	pci_console_nexus_de_init_resources(pdev);

	devm_kfree(dev, pci_cons_nexus);

	return 0;
}

static const struct of_device_id pci_console_nexus_of_match[] = {
	{ .compatible = "marvell,pci-console-nexus", },
	{},
};
MODULE_DEVICE_TABLE(of, pci_console_nexus_of_match);

static const struct platform_device_id pci_console_nexus_pdev_match[] = {
	{ .name = NEXUS_DRV_NAME, },
	{},
};
MODULE_DEVICE_TABLE(platform, pci_console_nexus_pdev_match);

static struct platform_driver pci_console_nexus_driver = {
	.driver = {
		.name = NEXUS_DRV_NAME,
		.of_match_table = pci_console_nexus_of_match,
	},
	.probe = pci_console_nexus_probe,
	.remove = pci_console_nexus_remove,
	.shutdown = pci_console_nexus_shutdown,
	.id_table = pci_console_nexus_pdev_match,
};

module_platform_driver(pci_console_nexus_driver);

MODULE_DESCRIPTION("OcteonTX PCI Console Driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" NEXUS_DRV_NAME);

/* =========================== pci console driver =========================== */

/*
 * Low-level initialization function for octeontx console state.
 */
static int octeontx_console_init(struct device *dev,
				 struct pci_console *pci_cons, int index,
				 u64 cons_addr, u64 cons_size)
{
	int ret;
	u32 cons_num;
	struct octeontx_pcie_console __iomem *ring_descr;

	/* see notes in structure declaration regarding these elements */
	BUILD_BUG_ON(offsetof(struct octeontx_pcie_console,
			      host_console_connected) & 0x7);
	BUILD_BUG_ON((offsetof(struct octeontx_pcie_console,
			       host_console_connected) + sizeof(u32)) !=
		     offsetof(struct octeontx_pcie_console, output_read_index));

	dbgmsg(dev, "%s: entry\n", __func__);

	ret = -ENODEV;
	cons_num = index;

	if (!cons_addr) {
		dev_err(dev, "Missing console base address\n");
		goto exit;
	}

	/* map the ring descriptor from the nexus */
	/* TODO: verify we can use WC */
	pci_cons->ring_descr = ioremap_wc(cons_addr, cons_size);
	if (!pci_cons->ring_descr) {
		dev_err(dev,
			"Unable to remap console %d base address\n",
			cons_num);
		goto exit;
	}

	ring_descr = pci_cons->ring_descr;

	/* Here, we verify/use existing configuration
	 * (i.e. from U-Boot).
	 *
	 * If this changes and the console is initialized here,
	 * then the pcie_lock must be taken/released around
	 * the init code.
	 */

	if (readq(&ring_descr->magic) !=
	    cpu_to_le64(OCTEONTX_PCIE_CONSOLE_MAGIC)) {
		dev_err(dev, "Invalid console %d signature\n",
			cons_num);
		goto exit;
	}

	/* Implementation note: using 'u32' will catch negative vals */
	if (((u32)le32_to_cpu(readl(&ring_descr->input_read_index)) >=
	     le32_to_cpu(readl(&ring_descr->input_buf_size))) ||
	    ((u32)le32_to_cpu(readl(&ring_descr->input_write_index)) >=
	     le32_to_cpu(readl(&ring_descr->input_buf_size))) ||
	    ((u32)le32_to_cpu(readl(&ring_descr->output_read_index)) >=
	     le32_to_cpu(readl(&ring_descr->output_buf_size))) ||
	    ((u32)le32_to_cpu(readl(&ring_descr->output_write_index)) >=
	     le32_to_cpu(readl(&ring_descr->output_buf_size))) ||
	    !readl(&ring_descr->input_buf_size) ||
	    !readl(&ring_descr->output_buf_size) ||
	    !readq(&ring_descr->input_base_addr) ||
	    !readq(&ring_descr->output_base_addr)) {
		dev_err(dev, "Invalid console %d ring configuration\n",
			cons_num);
		goto exit;
	}

	/* map the input buffer */
	pci_cons->input_ring = ioremap_wc(readq(&ring_descr->input_base_addr),
					  readl(&ring_descr->input_buf_size));

	/* map the output buffer */
	pci_cons->output_ring = ioremap_wc(readq(&ring_descr->output_base_addr),
					   readl(&ring_descr->output_buf_size));

	if (!pci_cons->input_ring || !pci_cons->output_ring) {
		dev_err(dev,
			"Unable to remap console %d memory ring[s]\n",
			cons_num);
		goto exit;
	}

	writel(cpu_to_le32(cons_num), &ring_descr->host.cons_idx);
	spin_lock_init(&pci_cons->excl_lock[cons_num]);

	ret = 0;

exit:
	return ret ? -ENODEV : 0;
}

/*
 * Low-level de-initialization function for octeontx console state.
 */
static int octeontx_console_de_init(struct device *dev,
				    struct pci_console *pci_cons, int index,
				    u64 cons_addr, u64 cons_size)
{
	dbgmsg(dev, "%s: entry\n", __func__);

	if (pci_cons->input_ring) {
		iounmap(pci_cons->input_ring);
		pci_cons->input_ring = NULL;
	}

	if (pci_cons->output_ring) {
		iounmap(pci_cons->output_ring);
		pci_cons->output_ring = NULL;
	}

	if (pci_cons->ring_descr) {
		iounmap(pci_cons->ring_descr);
		pci_cons->ring_descr = NULL;
	}

	return 0;
}

/*
 * Used to acquire or release a low-level octeontx console.
 */
static bool
octeontx_console_acquire(struct octeontx_pcie_console_nexus __iomem *nexus_desc,
			 int index, bool acquire, u64 *old, u64 *new)
{
	bool b_ok;
	u32 wait_usecs;
	u64 old_use_mask, new_use_mask;
	int cons_num;

	b_ok = false;

	if (!nexus_desc)
		return b_ok;

	cons_num = index;
	wait_usecs = 0;

#define CONSOLE_NEXUS_IN_USE_WAIT_USECS    1
#define CONSOLE_NEXUS_IN_USE_TIMEOUT_USECS 100

	do {
		old_use_mask = le32_to_cpu(readl(&nexus_desc->in_use)) |
			       (u64)le32_to_cpu(readl(
						&nexus_desc->exclusive)) << 32;

		/* set (or clear) both 'in-use' and 'exclusive' bits */
		new_use_mask = ((1ULL << cons_num) |
			       ((1ULL << cons_num) << 32));

		if (acquire) {
			/* Check if console has already been acquired */
			if (old_use_mask & (1ULL << cons_num))
				break;
			new_use_mask = old_use_mask | new_use_mask;
		} else {
			new_use_mask = old_use_mask & ~new_use_mask;
		}

		b_ok = (__atomic_compare_exchange_n((u64 *)&nexus_desc->in_use,
						    &old_use_mask, new_use_mask,
						    false, __ATOMIC_SEQ_CST,
						    __ATOMIC_SEQ_CST));
		if (b_ok)
			break;

		udelay(CONSOLE_NEXUS_IN_USE_WAIT_USECS);
		wait_usecs += CONSOLE_NEXUS_IN_USE_WAIT_USECS;

	} while (wait_usecs < CONSOLE_NEXUS_IN_USE_TIMEOUT_USECS);

	if (old)
		*old = old_use_mask;

	if (new)
		*new = new_use_mask;

	return b_ok;
}

/*
 * Clears pending data [bytes] from the low-level octeontx console output
 * buffer if the host console is not connected.
 * If the host console IS connected, an error is returned.
 *
 * @param console		console to clear output from
 * @param bytes_to_clear	Number of bytes to free up
 *
 * @return	0 for success, -1 on error, or a positive value
 *              If the size of pending data is less than 'bytes_to_clear',
 *              the return value equals the count of pending data.
 */
int octeontx_console_output_truncate(struct octeontx_pcie_console *console,
				     size_t bytes_to_clear)
{
	u64 old_val;
	u64 new_val;
	size_t bytes_avail;
	const u32 out_buf_size = le32_to_cpu(readl(&console->output_buf_size));
	u32 out_wr_idx, out_rd_idx;
	int ret;

	if (le32_to_cpu(readl(&console->host_console_connected)))
		return -1;

	out_wr_idx = le32_to_cpu(readl(&console->output_write_index));
	out_rd_idx = le32_to_cpu(readl(&console->output_read_index));

	old_val = cpu_to_le64((u64)out_rd_idx << 32);
	bytes_avail = buffer_pending_bytes(out_buf_size, out_wr_idx,
					   out_rd_idx);
	if (bytes_avail < 0)
		return bytes_avail;
	/* Not enough space */
	if (bytes_to_clear > bytes_avail)
		return bytes_avail;

	out_rd_idx = (out_rd_idx + bytes_to_clear) % out_buf_size;
	new_val = cpu_to_le64((u64)out_rd_idx << 32);

	/*
	 * We need to use an atomic operation here in case the host
	 * console should connect.  This guarantees that if the host
	 * connects that it will always see a consistent state.  Normally
	 * only the host can modify the read pointer.  This assures us
	 * that the read pointer will only be modified if the host
	 * is disconnected.
	 */
	ret = __atomic_compare_exchange_n
			((u64 *)(&console->host_console_connected),
			 &old_val, new_val, 0,
			 __ATOMIC_RELAXED, __ATOMIC_RELAXED);

	return ret ? 0 : -1;
}

/*
 * Low-level octeontx console write function.
 *
 * NOTE: this may NOT sleep, as it is called by the TTY 'write()' API.
 *
 */
static unsigned
octeontx_console_write(struct device *dev, const char *buf, unsigned int len,
		       struct octeontx_pcie_console __iomem *ring_descr,
		       u8 __iomem *output_ring, spinlock_t *excl_lock)
{
	const u8 *src;
	int srclen, avail, wr_len, written;
	unsigned int wait_usecs;
	u32 sz, rd_idx, wr_idx;

	spin_lock(excl_lock);

	sz = le32_to_cpu(readl(&ring_descr->output_buf_size));
	src = buf;
	srclen = len;
	written = 0;
	wait_usecs = 0;

	wr_idx = le32_to_cpu(readl(&ring_descr->output_write_index));

	while (srclen > 0) {
		rd_idx = le32_to_cpu(readl(&ring_descr->output_read_index));
		avail = buffer_free_bytes(sz, wr_idx, rd_idx);

		if (avail > 0) {
			/* reset host wait time */
			wait_usecs = 0;

			wr_len = min(avail, srclen);
			srclen -= wr_len;
			if (wr_idx + wr_len > sz) {
				memcpy_toio(output_ring + wr_idx, src,
					    (sz - wr_idx));
				wr_len -= (sz - wr_idx);
				src += (sz - wr_idx);
				wr_idx = 0;
			}
			if (wr_len)
				memcpy_toio(output_ring + wr_idx, src, wr_len);
			src += wr_len;
			written += wr_len;
			wr_idx = (wr_idx + wr_len) % sz;

			/* The write index is used by another process
			 * (remote PCI) to indicate the presence of [new] data
			 * in the ring buffer.
			 * Use a barrier here to ensure that all such data
			 * has been committed to memory prior to updating
			 * the write index in the descriptor.
			 */
			wmb();
			writel(cpu_to_le32(wr_idx),
			       &ring_descr->output_write_index);
		} else if (!avail) {
			/* Try to free space in output buffer (i.e. truncate) */
			wr_len = octeontx_console_output_truncate(ring_descr,
								  srclen);

			if (wr_len < 0) {
				if (wait_usecs >=
				    PCI_CONS_HOST_WAIT_TIMEOUT_USECS) {
					dev_err_once(dev,
						     "Timeout awaiting host\n");
					break;
				}
				/* We cannot sleep, we have acquired the lock */
				udelay(PCI_CONS_HOST_WAIT_LOOP_USECS);
				wait_usecs += PCI_CONS_HOST_WAIT_LOOP_USECS;
			} else if (wr_len > 0) {
				/* Truncate what we can */
				wr_len = octeontx_console_output_truncate(
						ring_descr, wr_len);
				if (wr_len != 0) {
					dev_err(dev,
						"output buffer truncate error\n");
					break;
				}
			}
		} else {
			dev_err_once(dev, "output buffer error\n");
			break;
		}
	}

	spin_unlock(excl_lock);

	return written;
}

/*
 * Linux console callback.
 */
static void pci_console_dev_write(struct console *cons, const char *buf,
				  unsigned int len)
{
	struct pci_console *pci_cons = cons->data;
	struct device *dev = pci_cons->device;
	struct octeontx_pcie_console __iomem *ring_descr;
	u8 __iomem *output_ring;
	u32 cons_idx;

	ring_descr = pci_cons->ring_descr;
	output_ring = pci_cons->output_ring;

	cons_idx = le32_to_cpu(readl(&ring_descr->host.cons_idx));

	octeontx_console_write(dev, buf, len, ring_descr, output_ring,
			       &pci_cons->excl_lock[cons_idx]);
}

/*
 * Linux console callback.
 */
static struct tty_driver *pci_console_dev_device(struct console *cons,
						 int *index)
{
	struct pci_console *pci_cons = cons->data;
	struct device *dev = pci_cons->device;

	dbgmsg(dev, "%s: entry\n", __func__);

	*index = pci_cons->cons.index;

	dbgmsg(dev, "return index: %d, tty driver: %p\n", *index,
	       pci_cons->tty.drv);

	return pci_cons->tty.drv;
}

/*
 * Linux console initialization callback.
 *
 * Create and register a TTY driver to be used with this console.
 *
 */
int pci_console_dev_setup(struct console *cons, char *arg)
{
	struct pci_console *pci_cons = cons->data;
	struct device *dev = pci_cons->device;
	struct tty_driver *ttydrv;
	int ret;

	dbgmsg(dev, "%s: entry, args '%s'\n", __func__, arg);

	ret = 0;
	ttydrv = NULL;

	/* Create/register our TTY driver */
	if (!pci_cons->tty.drv) {
		ret = -ENODEV;

		ttydrv = tty_alloc_driver(1 /*i.e. a single line */,
					  TTY_DRIVER_REAL_RAW);
		if (!ttydrv) {
			dev_err(dev, "Cannot allocate tty driver\n");
			goto exit;
		}

		ttydrv->driver_name = DRV_NAME;
		ttydrv->name = "ttyPCI";
		ttydrv->type = TTY_DRIVER_TYPE_SERIAL;
		ttydrv->subtype = SERIAL_TYPE_NORMAL;
		ttydrv->major = TTY_DRV_MAJOR_VER;
		ttydrv->minor_start = TTY_DRV_MINOR_VER_START;
		ttydrv->init_termios = tty_std_termios;
		ttydrv->init_termios.c_cflag =
			B9600 | CS8 | CREAD | HUPCL | CLOCAL;
		ttydrv->driver_state = pci_cons;
		tty_set_operations(ttydrv, &pci_console_dev_tty_ops);
		tty_port_init(&pci_cons->tty.port);
		tty_port_link_device(&pci_cons->tty.port, ttydrv,
				     0 /* i.e. the first, and only, port */);
		ret = tty_register_driver(ttydrv);
		if (ret) {
			dev_err(dev, "Error registering TTY %s\n",
				ttydrv->name);
			goto exit;
		}

		pci_cons->tty.drv = ttydrv;

		ret = 0;
	}

exit:
	/* If error initializing tty driver, release it */
	if (ret && ttydrv)
		put_tty_driver(ttydrv);

	return ret ? -ENODEV : 0;
}

/*
 * Main initialization function for pci_console device instance.
 *
 * returns:
 *   0 if no error
 *   -ENODEV if error occurred initializing device
 *   ENODEV if device should not be used (not an error per se)
 */
static int pci_console_init(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pci_console *pci_cons = platform_get_drvdata(pdev);
	struct device_node *of_node, *of_parent;
	int ret, cons_num, len;
	u64 cons_addr, cons_size, new_use_mask, old_use_mask;
	u64 of_parent_sz, of_parent_xbase;
	u32 cons_index;
	const __be32 *of_base, *of_parent_base;
	struct octeontx_pcie_console_nexus __iomem *nexus_desc;

	dbgmsg(dev, "%s: entry\n", __func__);

	ret = -ENODEV;

	nexus_desc = NULL;

	of_node = pdev->dev.of_node;
	if (!of_node) {
		dev_err(dev, "Missing devicetree configuration\n");
		goto exit;
	}

	/* retrieve our console index */
	cons_num = -1;
	if (!of_property_read_u32(of_node, "reg", &cons_index))
		cons_num = cons_index;
	if ((cons_num < 0) ||
	    (cons_num >= OCTEONTX_PCIE_MAX_CONSOLES)) {
		dev_err(dev, "Invalid configuration console index\n");
		goto exit;
	}

	if (!(max_cons_mask & BIT(cons_num))) {
		dev_info(dev, "Ignoring excluded console %d\n",
			 cons_num);
		ret = ENODEV;
		goto exit;
	}

	/* Retrieve console base address and length from device tree */
	cons_addr = OF_BAD_ADDR;
	of_base = of_get_address(of_node, 0, &cons_size, 0);
	if (of_base)
		cons_addr = of_translate_address(of_node, of_base);
	if (cons_addr == OF_BAD_ADDR) {
		dev_err(dev, "Invalid configuration base address\n");
		goto exit;
	}

	dbgmsg(dev, "Located console %d, address %#llx, size: %#llx\n",
	       cons_num, cons_addr, cons_size);

	/* ======================================================= */
	/* Note: we must [eventually] call 'of_node_put' on parent */
	of_parent = of_get_parent(of_node);
	if (!of_parent) {
		dev_err(dev,
			"Missing devicetree parent configuration\n");
		goto exit;
	}

	/* retrieve (and map) nexus pointer from parent node */
	of_parent_base = of_get_address(of_parent, 0, &of_parent_sz, 0);
	if (of_parent_base) {
		of_parent_xbase = of_translate_address(of_parent,
						       of_parent_base);
		/* TODO: verify we can use WC */
		if (of_parent_xbase != OF_BAD_ADDR) {
			dbgmsg(dev, "of_parent_xbase: %#llx\n",
			       of_parent_xbase);
			pci_cons->nexus_desc = nexus_desc =
				ioremap_wc(of_parent_xbase,
					   of_parent_sz);
		}
	}

	/* Release reference on parent */
	of_node_put(of_parent);
	/* ======================================================= */

	if (!nexus_desc) {
		dev_err(dev,
			"Invalid parent configuration base address\n");
		goto exit;
	}

	/* Verify/use existing configuration (i.e. from U-Boot) */

	if (readq(&nexus_desc->magic) !=
		cpu_to_le64(OCTEONTX_PCIE_CONSOLE_NEXUS_MAGIC)) {
		dev_err(dev, "Invalid nexus signature\n");
		goto exit;
	}

	if (cons_addr !=
	    le64_to_cpu(readq(&nexus_desc->console_addr[cons_num]))) {
		dev_err(dev,
			"Console %d base address mismatch %#llx/%#llx\n"
			, cons_num, cons_addr,
			le64_to_cpu(readq(&nexus_desc->console_addr[cons_num]))
			);
		goto exit;
	}

	if (le32_to_cpu(readl(&nexus_desc->in_use)) & (1 << cons_num)) {
		dev_err(dev, "Console %d already in-use\n", cons_num);
		goto exit;
	}

	if (octeontx_console_init(dev, pci_cons, cons_num, cons_addr,
				  cons_size)) {
		dev_err(dev,
			"Error initializing octeontx pci console\n");
		goto exit;
	}

	dev_info(dev,
		 "Initialized console %d, address %#llx, size: %#llx\n",
		 cons_num, cons_addr, cons_size);

	old_use_mask = new_use_mask = 0;

	if (!octeontx_console_acquire(pci_cons->nexus_desc, cons_num,
				      true, &old_use_mask,
				      &new_use_mask)) {
		dev_err(dev,
			"Console acquisition failed, old: %#llx, new: %#llx\n",
			old_use_mask, new_use_mask);
		goto exit;
	}

	pci_cons->octeontx_console_acquired = true;

	dbgmsg(dev, "Console acquisition - old: %#llx, new: %#llx\n",
	       old_use_mask, new_use_mask);

	/* initialize linux console state */
	len = sizeof(pci_cons->cons.name);
	strncpy(pci_cons->cons.name, "pci", len - 1);
	pci_cons->cons.name[len - 1] = 0;
	pci_cons->device = dev;
	pci_cons->cons.write = pci_console_dev_write;
	pci_cons->cons.device = pci_console_dev_device;
	pci_cons->cons.setup = pci_console_dev_setup;
	pci_cons->cons.data = pci_cons;
	pci_cons->cons.index = cons_num;
	pci_cons->cons.flags = CON_PRINTBUFFER;

	register_console(&pci_cons->cons);

	ret = 0;

exit:
	return ret;
}

/*
 * Main de-initialization function for pci_console device instance.
 */
static int pci_console_de_init(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pci_console *pci_cons = platform_get_drvdata(pdev);
	u64 new_use_mask, old_use_mask;
	int cons_num;

	dbgmsg(dev, "%s: entry\n", __func__);

	cons_num = pci_cons->cons.index;

	if (pci_cons->tty.drv) {
		tty_unregister_driver(pci_cons->tty.drv);
		put_tty_driver(pci_cons->tty.drv);
	}

	if (pci_cons->cons.flags & CON_ENABLED) {
		if (unregister_console(&pci_cons->cons))
			dev_err(dev,
				"Error unregistering pci console %d\n",
				cons_num);
	}

	octeontx_console_de_init(dev, pci_cons, cons_num, 0, 0);

	if (pci_cons->octeontx_console_acquired) {
		old_use_mask = new_use_mask = 0;
		if (!octeontx_console_acquire(pci_cons->nexus_desc,
				     cons_num, false, &old_use_mask,
				     &new_use_mask))
			dev_err(dev,
				"Console release failed, old: %#llx, new: %#llx\n",
				old_use_mask, new_use_mask);
		else
			dbgmsg(dev,
			       "Console release - old: %#llx, new: %#llx\n",
			       old_use_mask, new_use_mask);

		iounmap(pci_cons->nexus_desc);
		pci_cons->nexus_desc = NULL;
	}

	return 0;
}

static int pci_console_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pci_console *pci_cons;
	int ret;

	dbgmsg(dev, "%s: entry\n", __func__);

	pci_cons = NULL;

	ret = -ENODEV;

	/* allocate device structure */
	pci_cons = devm_kzalloc(dev, sizeof(*pci_cons), GFP_KERNEL);

	if (pci_cons == NULL) {
		ret = -ENOMEM;
		dev_err(dev, "Unable to allocate drv context.\n");
		goto exit;
	}

	platform_set_drvdata(pdev, pci_cons);

	ret = pci_console_init(pdev);

	/* a negative value indicates an error */
	if (ret < 0)
		dev_err(dev, "Error initializing pci console\n");

exit:
	if (ret) {
		pci_console_de_init(pdev);

		if (pci_cons != NULL)
			devm_kfree(dev, pci_cons);
	}

	return ret ? -ENODEV : 0;
}

static void pci_console_shutdown(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;

	dbgmsg(dev, "%s: entry\n", __func__);
}

static int pci_console_remove(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pci_console *pci_cons = platform_get_drvdata(pdev);

	dbgmsg(dev, "%s: entry\n", __func__);

	pci_console_de_init(pdev);

	devm_kfree(dev, pci_cons);

	return 0;
}

static const struct of_device_id pci_console_of_match[] = {
	{ .compatible = "marvell,pci-console", },
	{},
};
MODULE_DEVICE_TABLE(of, pci_console_of_match);

static const struct platform_device_id pci_console_pdev_match[] = {
	{ .name = DRV_NAME, },
	{},
};
MODULE_DEVICE_TABLE(platform, pci_console_pdev_match);

static struct platform_driver pci_console_driver = {
	.driver = {
		.name = DRV_NAME,
		.of_match_table = pci_console_of_match,
	},
	.probe = pci_console_probe,
	.remove = pci_console_remove,
	.shutdown = pci_console_shutdown,
	.id_table = pci_console_pdev_match,
};

/* ========================= pci console TTY driver ========================= */

/*
 * Linux TTY driver timer callback (used to poll for data).
 */
void pci_console_dev_tty_poll(struct timer_list *timer)
{
#define MAX_BUFFERED_INP_DATA 0x100
	struct pci_console *pci_cons = from_timer(pci_cons, timer, tty.poll_timer);
	struct octeontx_pcie_console __iomem *ring_descr;
	u8 __iomem *input_ring;
	u8 buf[MAX_BUFFERED_INP_DATA];
	int cnt;
	u32 sz, rd_idx, wr_idx, avail;

	BUILD_BUG_ON(PCI_CONS_TTY_POLL_INTERVAL_JIFFIES > HZ);
	if (!(pci_cons->tty.stats.poll_count++ %
	      (HZ / PCI_CONS_TTY_POLL_INTERVAL_JIFFIES))) {
		dbgmsg(pci_cons->device,
		       "timer poll count: %u, dropped: %u, pushed: %u\n",
		       pci_cons->tty.stats.poll_count,
		       pci_cons->tty.stats.dropped_count,
		       pci_cons->tty.stats.pushed_count);
	}

	ring_descr = pci_cons->ring_descr;
	input_ring = pci_cons->input_ring;
	sz = le32_to_cpu(readl(&ring_descr->input_buf_size));
	rd_idx = le32_to_cpu(readl(&ring_descr->input_read_index));
	wr_idx = le32_to_cpu(readl(&ring_descr->input_write_index));
	avail = buffer_pending_bytes(sz, wr_idx, rd_idx);

	while ((s32)avail > 0) {

		if (rd_idx > wr_idx)
			cnt = min(avail, sz - rd_idx);
		else
			cnt = min(avail, wr_idx - rd_idx);

		cnt = min(cnt, MAX_BUFFERED_INP_DATA);
		memcpy_fromio(buf, &input_ring[rd_idx], cnt);
		cnt = tty_insert_flip_string(&pci_cons->tty.port, buf, cnt);
		if (!cnt) {
			pci_cons->tty.stats.dropped_count += cnt;
			break;
		}

		rd_idx = (rd_idx + cnt) % sz;
		avail -= cnt;

		pci_cons->tty.stats.pushed_count += cnt;

		tty_flip_buffer_push(&pci_cons->tty.port);
	}
	/* The read index is used by another process (remote PCI) to
	 * indicate which data have been consumed from the ring buffer.
	 * Use a barrier here to ensure that all such data
	 * has been copied from the ring buffer prior to updating the
	 * read index in the descriptor.
	 */
	mb();
	writel(cpu_to_le32(rd_idx), &ring_descr->input_read_index);

	mod_timer(&pci_cons->tty.poll_timer,
		  jiffies + PCI_CONS_TTY_POLL_INTERVAL_JIFFIES);
}

/*
 * Linux TTY driver callback.
 */
static int pci_console_dev_tty_open(struct tty_struct *tty, struct file *filp)
{
	struct pci_console *pci_cons = tty->driver->driver_state;
	struct device *dev = pci_cons->device;

	dbgmsg(dev, "%s: entry\n", __func__);

	if (!pci_cons->tty.open_count++) {
		dbgmsg(dev, "Scheduling timer...\n");
		timer_setup(&pci_cons->tty.poll_timer,
			    pci_console_dev_tty_poll, 0);
		mod_timer(&pci_cons->tty.poll_timer,
			  jiffies + PCI_CONS_TTY_POLL_INTERVAL_JIFFIES);
	}

	return 0;
}

/*
 * Linux TTY driver callback.
 */
static void pci_console_dev_tty_close(struct tty_struct *tty,
				      struct file *filp)
{
	struct pci_console *pci_cons = tty->driver->driver_state;
	struct device *dev = pci_cons->device;

	dbgmsg(dev, "%s: entry\n", __func__);

	if (--pci_cons->tty.open_count == 0) {
		dbgmsg(dev, "Deleting timer...\n");
		del_timer(&pci_cons->tty.poll_timer);
	}
}

/*
 * Linux TTY driver callback.
 */
static int pci_console_dev_tty_write(struct tty_struct *tty,
				     const unsigned char *buf, int count)
{
	struct pci_console *pci_cons = tty->driver->driver_state;
	struct device *dev = pci_cons->device;
	struct octeontx_pcie_console __iomem *ring_descr;
	u8 __iomem *output_ring;
	u32 cons_idx;

	ring_descr = pci_cons->ring_descr;
	output_ring = pci_cons->output_ring;

	cons_idx = le32_to_cpu(readl(&ring_descr->host.cons_idx));

	return octeontx_console_write(dev, buf, count, ring_descr,
				      output_ring,
				      &pci_cons->excl_lock[cons_idx]);
}

static int pci_console_dev_tty_write_room(struct tty_struct *tty)
{
	struct pci_console *pci_cons = tty->driver->driver_state;

	/* Assume maximum space is available; write function will wait for
	 * available room, if necessary.
	 */
	return pci_cons->ring_descr->output_buf_size - 1;
}

static int pci_console_dev_tty_chars_in_buffer(struct tty_struct *tty)
{
	struct pci_console *pci_cons = tty->driver->driver_state;

	(void)pci_cons;

	/* We do not buffer any data - zero chars in buffer */
	return 0;
}

static void pci_console_dev_tty_send_xchar(struct tty_struct *tty, char ch)
{
	pci_console_dev_tty_write(tty, (const u8 *)&ch, sizeof(ch));
}