summaryrefslogtreecommitdiffstats
path: root/sys/sparc64/pci/sbbc.c
blob: f1e7fab94cedd227deb520ab14362d2775613d01 (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
/*	$OpenBSD: sbbc.c,v 1.7 2009/11/09 17:53:39 nicm Exp $	*/
/*-
 * Copyright (c) 2008 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/*-
 * Copyright (c) 2010 Marius Strobl <marius@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/clock.h>
#include <sys/endian.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/resource.h>
#include <sys/rman.h>

#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/openfirm.h>

#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/resource.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/uart/uart.h>
#include <dev/uart/uart_cpu.h>
#include <dev/uart/uart_bus.h>

#include "clock_if.h"
#include "uart_if.h"

#define	SBBC_PCI_BAR		PCIR_BAR(0)
#define	SBBC_PCI_VENDOR		0x108e
#define	SBBC_PCI_PRODUCT	0xc416

#define	SBBC_REGS_OFFSET	0x800000
#define	SBBC_REGS_SIZE		0x6230
#define	SBBC_EPLD_OFFSET	0x8e0000
#define	SBBC_EPLD_SIZE		0x20
#define	SBBC_SRAM_OFFSET	0x900000
#define	SBBC_SRAM_SIZE		0x20000	/* 128KB SRAM */

#define	SBBC_PCI_INT_STATUS	0x2320
#define	SBBC_PCI_INT_ENABLE	0x2330
#define	SBBC_PCI_ENABLE_INT_A	0x11

#define	SBBC_EPLD_INTERRUPT	0x13
#define	SBBC_EPLD_INTERRUPT_ON	0x01

#define	SBBC_SRAM_CONS_IN		0x00000001
#define	SBBC_SRAM_CONS_OUT		0x00000002
#define	SBBC_SRAM_CONS_BRK		0x00000004
#define	SBBC_SRAM_CONS_SPACE_IN		0x00000008
#define	SBBC_SRAM_CONS_SPACE_OUT	0x00000010

#define	SBBC_TAG_KEY_SIZE	8
#define	SBBC_TAG_KEY_SCSOLIE	"SCSOLIE"	/* SC -> OS int. enable */
#define	SBBC_TAG_KEY_SCSOLIR	"SCSOLIR"	/* SC -> OS int. reason */
#define	SBBC_TAG_KEY_SOLCONS	"SOLCONS"	/* OS console buffer */
#define	SBBC_TAG_KEY_SOLSCIE	"SOLSCIE"	/* OS -> SC int. enable */
#define	SBBC_TAG_KEY_SOLSCIR	"SOLSCIR"	/* OS -> SC int. reason */
#define	SBBC_TAG_KEY_TODDATA	"TODDATA"	/* OS TOD struct */
#define	SBBC_TAG_OFF(x)		offsetof(struct sbbc_sram_tag, x)

struct sbbc_sram_tag {
	char		tag_key[SBBC_TAG_KEY_SIZE];
	uint32_t	tag_size;
	uint32_t	tag_offset;
} __packed;

#define	SBBC_TOC_MAGIC		"TOCSRAM"
#define	SBBC_TOC_MAGIC_SIZE	8
#define	SBBC_TOC_TAGS_MAX	32
#define	SBBC_TOC_OFF(x)		offsetof(struct sbbc_sram_toc, x)

struct sbbc_sram_toc {
	char			toc_magic[SBBC_TOC_MAGIC_SIZE];
	uint8_t			toc_reserved;
	uint8_t			toc_type;
	uint16_t		toc_version;
	uint32_t		toc_ntags;
	struct sbbc_sram_tag	toc_tag[SBBC_TOC_TAGS_MAX];
} __packed;

#define	SBBC_TOD_MAGIC		0x54443100	/* "TD1" */
#define	SBBC_TOD_VERSION	1
#define	SBBC_TOD_OFF(x)		offsetof(struct sbbc_sram_tod, x)

struct sbbc_sram_tod {
	uint32_t	tod_magic;
	uint32_t	tod_version;
	uint64_t	tod_time;
	uint64_t	tod_skew;
	uint32_t	tod_reserved;
	uint32_t	tod_heartbeat;
	uint32_t	tod_timeout;
} __packed;

#define	SBBC_CONS_MAGIC		0x434f4e00	/* "CON" */
#define	SBBC_CONS_VERSION	1
#define	SBBC_CONS_OFF(x)	offsetof(struct sbbc_sram_cons, x)

struct sbbc_sram_cons {
	uint32_t cons_magic;
	uint32_t cons_version;
	uint32_t cons_size;

	uint32_t cons_in_begin;
	uint32_t cons_in_end;
	uint32_t cons_in_rdptr;
	uint32_t cons_in_wrptr;

	uint32_t cons_out_begin;
	uint32_t cons_out_end;
	uint32_t cons_out_rdptr;
	uint32_t cons_out_wrptr;
} __packed;

struct sbbc_softc {
	struct resource *sc_res;
};

#define	SBBC_READ_N(wdth, offs)						\
	bus_space_read_ ## wdth((bst), (bsh), (offs))
#define	SBBC_WRITE_N(wdth, offs, val)					\
	bus_space_write_ ## wdth((bst), (bsh), (offs), (val))

#define	SBBC_READ_1(offs)						\
	SBBC_READ_N(1, (offs))
#define	SBBC_READ_2(offs)						\
	bswap16(SBBC_READ_N(2, (offs)))
#define	SBBC_READ_4(offs)						\
	bswap32(SBBC_READ_N(4, (offs)))
#define	SBBC_READ_8(offs)						\
	bswap64(SBBC_READ_N(8, (offs)))
#define	SBBC_WRITE_1(offs, val)						\
	SBBC_WRITE_N(1, (offs), (val))
#define	SBBC_WRITE_2(offs, val)						\
	SBBC_WRITE_N(2, (offs), bswap16(val))
#define	SBBC_WRITE_4(offs, val)						\
	SBBC_WRITE_N(4, (offs), bswap32(val))
#define	SBBC_WRITE_8(offs, val)						\
	SBBC_WRITE_N(8, (offs), bswap64(val))

#define	SBBC_REGS_READ_1(offs)						\
	SBBC_READ_1((offs) + SBBC_REGS_OFFSET)
#define	SBBC_REGS_READ_2(offs)						\
	SBBC_READ_2((offs) + SBBC_REGS_OFFSET)
#define	SBBC_REGS_READ_4(offs)						\
	SBBC_READ_4((offs) + SBBC_REGS_OFFSET)
#define	SBBC_REGS_READ_8(offs)						\
	SBBC_READ_8((offs) + SBBC_REGS_OFFSET)
#define	SBBC_REGS_WRITE_1(offs, val)					\
	SBBC_WRITE_1((offs) + SBBC_REGS_OFFSET, (val))
#define	SBBC_REGS_WRITE_2(offs, val)					\
	SBBC_WRITE_2((offs) + SBBC_REGS_OFFSET, (val))
#define	SBBC_REGS_WRITE_4(offs, val)					\
	SBBC_WRITE_4((offs) + SBBC_REGS_OFFSET, (val))
#define	SBBC_REGS_WRITE_8(offs, val)					\
	SBBC_WRITE_8((offs) + SBBC_REGS_OFFSET, (val))

#define	SBBC_EPLD_READ_1(offs)						\
	SBBC_READ_1((offs) + SBBC_EPLD_OFFSET)
#define	SBBC_EPLD_READ_2(offs)						\
	SBBC_READ_2((offs) + SBBC_EPLD_OFFSET)
#define	SBBC_EPLD_READ_4(offs)						\
	SBBC_READ_4((offs) + SBBC_EPLD_OFFSET)
#define	SBBC_EPLD_READ_8(offs)						\
	SBBC_READ_8((offs) + SBBC_EPLD_OFFSET)
#define	SBBC_EPLD_WRITE_1(offs, val)					\
	SBBC_WRITE_1((offs) + SBBC_EPLD_OFFSET, (val))
#define	SBBC_EPLD_WRITE_2(offs, val)					\
	SBBC_WRITE_2((offs) + SBBC_EPLD_OFFSET, (val))
#define	SBBC_EPLD_WRITE_4(offs, val)					\
	SBBC_WRITE_4((offs) + SBBC_EPLD_OFFSET, (val))
#define	SBBC_EPLD_WRITE_8(offs, val)					\
	SBBC_WRITE_8((offs) + SBBC_EPLD_OFFSET, (val))

#define	SBBC_SRAM_READ_1(offs)						\
	SBBC_READ_1((offs) + SBBC_SRAM_OFFSET)
#define	SBBC_SRAM_READ_2(offs)						\
	SBBC_READ_2((offs) + SBBC_SRAM_OFFSET)
#define	SBBC_SRAM_READ_4(offs)						\
	SBBC_READ_4((offs) + SBBC_SRAM_OFFSET)
#define	SBBC_SRAM_READ_8(offs)						\
	SBBC_READ_8((offs) + SBBC_SRAM_OFFSET)
#define	SBBC_SRAM_WRITE_1(offs, val)					\
	SBBC_WRITE_1((offs) + SBBC_SRAM_OFFSET, (val))
#define	SBBC_SRAM_WRITE_2(offs, val)					\
	SBBC_WRITE_2((offs) + SBBC_SRAM_OFFSET, (val))
#define	SBBC_SRAM_WRITE_4(offs, val)					\
	SBBC_WRITE_4((offs) + SBBC_SRAM_OFFSET, (val))
#define	SBBC_SRAM_WRITE_8(offs, val)					\
	SBBC_WRITE_8((offs) + SBBC_SRAM_OFFSET, (val))

#define	SUNW_SETCONSINPUT	"SUNW,set-console-input"
#define	SUNW_SETCONSINPUT_CLNT	"CON_CLNT"
#define	SUNW_SETCONSINPUT_OBP	"CON_OBP"

static u_int sbbc_console;

static uint32_t	sbbc_scsolie;
static uint32_t	sbbc_scsolir;
static uint32_t	sbbc_solcons;
static uint32_t	sbbc_solscie;
static uint32_t	sbbc_solscir;
static uint32_t	sbbc_toddata;

/*
 * internal helpers
 */
static int sbbc_parse_toc(bus_space_tag_t bst, bus_space_handle_t bsh);
static inline void sbbc_send_intr(bus_space_tag_t bst,
    bus_space_handle_t bsh);
static const char *sbbc_serengeti_set_console_input(char *new);

/*
 * SBBC PCI interface
 */
static bus_activate_resource_t sbbc_bus_activate_resource;
static bus_adjust_resource_t sbbc_bus_adjust_resource;
static bus_deactivate_resource_t sbbc_bus_deactivate_resource;
static bus_alloc_resource_t sbbc_bus_alloc_resource;
static bus_release_resource_t sbbc_bus_release_resource;
static bus_get_resource_list_t sbbc_bus_get_resource_list;
static bus_setup_intr_t sbbc_bus_setup_intr;
static bus_teardown_intr_t sbbc_bus_teardown_intr;

static device_attach_t sbbc_pci_attach;
static device_probe_t sbbc_pci_probe;

static clock_gettime_t sbbc_tod_gettime;
static clock_settime_t sbbc_tod_settime;

static device_method_t sbbc_pci_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		sbbc_pci_probe),
	DEVMETHOD(device_attach,	sbbc_pci_attach),

	DEVMETHOD(bus_alloc_resource,	sbbc_bus_alloc_resource),
	DEVMETHOD(bus_activate_resource,sbbc_bus_activate_resource),
	DEVMETHOD(bus_deactivate_resource,sbbc_bus_deactivate_resource),
	DEVMETHOD(bus_adjust_resource,	sbbc_bus_adjust_resource),
	DEVMETHOD(bus_release_resource,	sbbc_bus_release_resource),
	DEVMETHOD(bus_setup_intr,	sbbc_bus_setup_intr),
	DEVMETHOD(bus_teardown_intr,	sbbc_bus_teardown_intr),
	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
	DEVMETHOD(bus_get_resource_list, sbbc_bus_get_resource_list),

	/* clock interface */
	DEVMETHOD(clock_gettime,	sbbc_tod_gettime),
	DEVMETHOD(clock_settime,	sbbc_tod_settime),

	DEVMETHOD_END
};

static devclass_t sbbc_devclass;

DEFINE_CLASS_0(sbbc, sbbc_driver, sbbc_pci_methods, sizeof(struct sbbc_softc));
DRIVER_MODULE(sbbc, pci, sbbc_driver, sbbc_devclass, NULL, NULL);

static int
sbbc_pci_probe(device_t dev)
{

	if (pci_get_vendor(dev) == SBBC_PCI_VENDOR &&
	    pci_get_device(dev) == SBBC_PCI_PRODUCT) {
		device_set_desc(dev, "Sun BootBus controller");
		return (BUS_PROBE_DEFAULT);
	}
	return (ENXIO);
}

static int
sbbc_pci_attach(device_t dev)
{
	struct sbbc_softc *sc;
	struct timespec ts;
	device_t child;
	bus_space_tag_t bst;
	bus_space_handle_t bsh;
	phandle_t node;
	int error, rid;
	uint32_t val;

	/* Nothing to to if we're not the chosen one. */
	if ((node = OF_finddevice("/chosen")) == -1) {
		device_printf(dev, "failed to find /chosen\n");
		return (ENXIO);
	}
	if (OF_getprop(node, "iosram", &node, sizeof(node)) == -1) {
		device_printf(dev, "failed to get iosram\n");
		return (ENXIO);
	}
	if (node != ofw_bus_get_node(dev))
		return (0);

	sc = device_get_softc(dev);
	rid = SBBC_PCI_BAR;
	sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
	    RF_ACTIVE);
	if (sc->sc_res == NULL) {
		device_printf(dev, "failed to allocate resources\n");
		return (ENXIO);
	}
	bst = rman_get_bustag(sc->sc_res);
	bsh = rman_get_bushandle(sc->sc_res);
	if (sbbc_console != 0) {
		/* Once again the interrupt pin isn't set. */
		if (pci_get_intpin(dev) == 0)
			pci_set_intpin(dev, 1);
		child = device_add_child(dev, NULL, -1);
		if (child == NULL)
			device_printf(dev, "failed to add UART device\n");
		error = bus_generic_attach(dev);
		if (error != 0)
			device_printf(dev, "failed to attach UART device\n");
	} else {
		error = sbbc_parse_toc(bst, bsh);
		if (error != 0) {
			device_printf(dev, "failed to parse TOC\n");
			if (sbbc_console != 0) {
				bus_release_resource(dev, SYS_RES_MEMORY, rid,
				    sc->sc_res);
				return (error);
			}
		}
	}
	if (sbbc_toddata != 0) {
		if ((val = SBBC_SRAM_READ_4(sbbc_toddata +
		    SBBC_TOD_OFF(tod_magic))) != SBBC_TOD_MAGIC)
			device_printf(dev, "invalid TOD magic %#x\n", val);
		else if ((val = SBBC_SRAM_READ_4(sbbc_toddata +
		    SBBC_TOD_OFF(tod_version))) < SBBC_TOD_VERSION)
			device_printf(dev, "invalid TOD version %#x\n", val);
		else {
			clock_register(dev, 1000000); /* 1 sec. resolution */
			if (bootverbose) {
				sbbc_tod_gettime(dev, &ts);
				device_printf(dev,
				    "current time: %ld.%09ld\n",
				    (long)ts.tv_sec, ts.tv_nsec);
			}
		}
	}
	return (0);
}

/*
 * Note that the bus methods don't pass-through the uart(4) requests but act
 * as if they would come from sbbc(4) in order to avoid complications with
 * pci(4) (actually, uart(4) isn't a real child but rather a function of
 * sbbc(4) anyway).
 */

static struct resource *
sbbc_bus_alloc_resource(device_t dev, device_t child __unused, int type,
    int *rid, u_long start, u_long end, u_long count, u_int flags)
{
	struct sbbc_softc *sc;

	sc = device_get_softc(dev);
	switch (type) {
	case SYS_RES_IRQ:
		return (bus_generic_alloc_resource(dev, dev, type, rid, start,
		    end, count, flags));
	case SYS_RES_MEMORY:
		return (sc->sc_res);
	default:
		return (NULL);
	}
}

static int
sbbc_bus_activate_resource(device_t bus, device_t child, int type, int rid,
    struct resource *res)
{

	if (type == SYS_RES_MEMORY)
		return (0);
	return (bus_generic_activate_resource(bus, child, type, rid, res));
}

static int
sbbc_bus_deactivate_resource(device_t bus, device_t child, int type, int rid,
    struct resource *res)
{

	if (type == SYS_RES_MEMORY)
		return (0);
	return (bus_generic_deactivate_resource(bus, child, type, rid, res));
}

static int
sbbc_bus_adjust_resource(device_t bus __unused, device_t child __unused,
    int type __unused, struct resource *res __unused, u_long start __unused,
    u_long end __unused)
{

	return (ENXIO);
}

static int
sbbc_bus_release_resource(device_t dev, device_t child __unused, int type,
    int rid, struct resource *res)
{

	if (type == SYS_RES_IRQ)
		return (bus_generic_release_resource(dev, dev, type, rid,
		    res));
	return (0);
}

static struct resource_list *
sbbc_bus_get_resource_list(device_t dev, device_t child __unused)
{

	return (bus_generic_get_resource_list(dev, dev));
}

static int
sbbc_bus_setup_intr(device_t dev, device_t child __unused,
    struct resource *res, int flags, driver_filter_t *filt,
    driver_intr_t *intr, void *arg, void **cookiep)
{

	return (bus_generic_setup_intr(dev, dev, res, flags, filt, intr, arg,
	    cookiep));
}

static int
sbbc_bus_teardown_intr(device_t dev, device_t child __unused,
    struct resource *res, void *cookie)
{

	return (bus_generic_teardown_intr(dev, dev, res, cookie));
}

/*
 * internal helpers
 */
static int
sbbc_parse_toc(bus_space_tag_t bst, bus_space_handle_t bsh)
{
	char buf[MAX(SBBC_TAG_KEY_SIZE, SBBC_TOC_MAGIC_SIZE)];
	bus_size_t tag;
	phandle_t node;
	uint32_t off, sram_toc;
	u_int i, tags;

	if ((node = OF_finddevice("/chosen")) == -1)
		return (ENXIO);
	/* SRAM TOC offset defaults to 0. */
	if (OF_getprop(node, "iosram-toc", &sram_toc, sizeof(sram_toc)) <= 0)
		sram_toc = 0;

	bus_space_read_region_1(bst, bsh, SBBC_SRAM_OFFSET + sram_toc +
	    SBBC_TOC_OFF(toc_magic), buf, SBBC_TOC_MAGIC_SIZE);
	buf[SBBC_TOC_MAGIC_SIZE - 1] = '\0';
	if (strcmp(buf, SBBC_TOC_MAGIC) != 0)
		return (ENXIO);

	tags = SBBC_SRAM_READ_4(sram_toc + SBBC_TOC_OFF(toc_ntags));
	for (i = 0; i < tags; i++) {
		tag = sram_toc + SBBC_TOC_OFF(toc_tag) +
		    i * sizeof(struct sbbc_sram_tag);
		bus_space_read_region_1(bst, bsh, SBBC_SRAM_OFFSET + tag +
		    SBBC_TAG_OFF(tag_key), buf, SBBC_TAG_KEY_SIZE);
		buf[SBBC_TAG_KEY_SIZE - 1] = '\0';
		off = SBBC_SRAM_READ_4(tag + SBBC_TAG_OFF(tag_offset));
		if (strcmp(buf, SBBC_TAG_KEY_SCSOLIE) == 0)
			sbbc_scsolie = off;
		else if (strcmp(buf, SBBC_TAG_KEY_SCSOLIR) == 0)
			sbbc_scsolir = off;
		else if (strcmp(buf, SBBC_TAG_KEY_SOLCONS) == 0)
			sbbc_solcons = off;
		else if (strcmp(buf, SBBC_TAG_KEY_SOLSCIE) == 0)
			sbbc_solscie = off;
		else if (strcmp(buf, SBBC_TAG_KEY_SOLSCIR) == 0)
			sbbc_solscir = off;
		else if (strcmp(buf, SBBC_TAG_KEY_TODDATA) == 0)
			sbbc_toddata = off;
	}
	return (0);
}

static const char *
sbbc_serengeti_set_console_input(char *new)
{
	struct {
		cell_t name;
		cell_t nargs;
		cell_t nreturns;
		cell_t new;
		cell_t old;
	} args = {
		(cell_t)SUNW_SETCONSINPUT,
		1,
		1,
	};

	args.new = (cell_t)new;
	if (ofw_entry(&args) == -1)
		return (NULL);
	return ((const char *)args.old);
}

static inline void
sbbc_send_intr(bus_space_tag_t bst, bus_space_handle_t bsh)
{

	SBBC_EPLD_WRITE_1(SBBC_EPLD_INTERRUPT, SBBC_EPLD_INTERRUPT_ON);
	bus_space_barrier(bst, bsh, SBBC_EPLD_OFFSET + SBBC_EPLD_INTERRUPT, 1,
	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
}

/*
 * TOD interface
 */
static int
sbbc_tod_gettime(device_t dev, struct timespec *ts)
{
	struct sbbc_softc *sc;
	bus_space_tag_t bst;
	bus_space_handle_t bsh;

	sc = device_get_softc(dev);
	bst = rman_get_bustag(sc->sc_res);
	bsh = rman_get_bushandle(sc->sc_res);

	ts->tv_sec = SBBC_SRAM_READ_8(sbbc_toddata + SBBC_TOD_OFF(tod_time)) +
	    SBBC_SRAM_READ_8(sbbc_toddata + SBBC_TOD_OFF(tod_skew));
	ts->tv_nsec = 0;
	return (0);
}

static int
sbbc_tod_settime(device_t dev, struct timespec *ts)
{
	struct sbbc_softc *sc;
	bus_space_tag_t bst;
	bus_space_handle_t bsh;

	sc = device_get_softc(dev);
	bst = rman_get_bustag(sc->sc_res);
	bsh = rman_get_bushandle(sc->sc_res);

	SBBC_SRAM_WRITE_8(sbbc_toddata + SBBC_TOD_OFF(tod_skew), ts->tv_sec -
	    SBBC_SRAM_READ_8(sbbc_toddata + SBBC_TOD_OFF(tod_time)));
	return (0);
}

/*
 * UART bus front-end
 */
static device_probe_t sbbc_uart_sbbc_probe;

static device_method_t sbbc_uart_sbbc_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		sbbc_uart_sbbc_probe),
	DEVMETHOD(device_attach,	uart_bus_attach),
	DEVMETHOD(device_detach,	uart_bus_detach),

	DEVMETHOD_END
};

DEFINE_CLASS_0(uart, sbbc_uart_driver, sbbc_uart_sbbc_methods,
    sizeof(struct uart_softc));
DRIVER_MODULE(uart, sbbc, sbbc_uart_driver, uart_devclass, NULL, NULL);

static int
sbbc_uart_sbbc_probe(device_t dev)
{
	struct uart_softc *sc;

	sc = device_get_softc(dev);
	sc->sc_class = &uart_sbbc_class;
	device_set_desc(dev, "Serengeti console");
	return (uart_bus_probe(dev, 0, 0, SBBC_PCI_BAR, 0));
}

/*
 * Low-level UART interface
 */
static int sbbc_uart_probe(struct uart_bas *bas);
static void sbbc_uart_init(struct uart_bas *bas, int baudrate, int databits,
    int stopbits, int parity);
static void sbbc_uart_term(struct uart_bas *bas);
static void sbbc_uart_putc(struct uart_bas *bas, int c);
static int sbbc_uart_rxready(struct uart_bas *bas);
static int sbbc_uart_getc(struct uart_bas *bas, struct mtx *hwmtx);

static struct uart_ops sbbc_uart_ops = {
	.probe = sbbc_uart_probe,
	.init = sbbc_uart_init,
	.term = sbbc_uart_term,
	.putc = sbbc_uart_putc,
	.rxready = sbbc_uart_rxready,
	.getc = sbbc_uart_getc,
};

static int
sbbc_uart_probe(struct uart_bas *bas)
{
	bus_space_tag_t bst;
	bus_space_handle_t bsh;
	int error;

	sbbc_console = 1;
	bst = bas->bst;
	bsh = bas->bsh;
	error = sbbc_parse_toc(bst, bsh);
	if (error != 0)
		return (error);

	if (sbbc_scsolie == 0 || sbbc_scsolir == 0 || sbbc_solcons == 0 ||
	    sbbc_solscie == 0 || sbbc_solscir == 0)
		return (ENXIO);

	if (SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_magic)) !=
	    SBBC_CONS_MAGIC || SBBC_SRAM_READ_4(sbbc_solcons +
	    SBBC_CONS_OFF(cons_version)) < SBBC_CONS_VERSION)
		return (ENXIO);
	return (0);
}

static void
sbbc_uart_init(struct uart_bas *bas, int baudrate __unused,
    int databits __unused, int stopbits __unused, int parity __unused)
{
	bus_space_tag_t bst;
	bus_space_handle_t bsh;

	bst = bas->bst;
	bsh = bas->bsh;

	/* Enable output to and space in from the SC interrupts. */
	SBBC_SRAM_WRITE_4(sbbc_solscie, SBBC_SRAM_READ_4(sbbc_solscie) |
	    SBBC_SRAM_CONS_OUT | SBBC_SRAM_CONS_SPACE_IN);
	uart_barrier(bas);

	/* Take over the console input. */
	sbbc_serengeti_set_console_input(SUNW_SETCONSINPUT_CLNT);
}

static void
sbbc_uart_term(struct uart_bas *bas __unused)
{

	/* Give back the console input. */
	sbbc_serengeti_set_console_input(SUNW_SETCONSINPUT_OBP);
}

static void
sbbc_uart_putc(struct uart_bas *bas, int c)
{
	bus_space_tag_t bst;
	bus_space_handle_t bsh;
	uint32_t wrptr;

	bst = bas->bst;
	bsh = bas->bsh;

	wrptr = SBBC_SRAM_READ_4(sbbc_solcons +
	    SBBC_CONS_OFF(cons_out_wrptr));
	SBBC_SRAM_WRITE_1(sbbc_solcons + wrptr, c);
	uart_barrier(bas);
	if (++wrptr == SBBC_SRAM_READ_4(sbbc_solcons +
	    SBBC_CONS_OFF(cons_out_end)))
		wrptr = SBBC_SRAM_READ_4(sbbc_solcons +
		    SBBC_CONS_OFF(cons_out_begin));
	SBBC_SRAM_WRITE_4(sbbc_solcons + SBBC_CONS_OFF(cons_out_wrptr),
	    wrptr);
	uart_barrier(bas);

	SBBC_SRAM_WRITE_4(sbbc_solscir, SBBC_SRAM_READ_4(sbbc_solscir) |
	    SBBC_SRAM_CONS_OUT);
	uart_barrier(bas);
	sbbc_send_intr(bst, bsh);
}

static int
sbbc_uart_rxready(struct uart_bas *bas)
{
	bus_space_tag_t bst;
	bus_space_handle_t bsh;

	bst = bas->bst;
	bsh = bas->bsh;

	if (SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_in_rdptr)) ==
	    SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_in_wrptr)))
		return (0);
	return (1);
}

static int
sbbc_uart_getc(struct uart_bas *bas, struct mtx *hwmtx)
{
	bus_space_tag_t bst;
	bus_space_handle_t bsh;
	int c;
	uint32_t rdptr;

	bst = bas->bst;
	bsh = bas->bsh;

	uart_lock(hwmtx);

	while (sbbc_uart_rxready(bas) == 0) {
		uart_unlock(hwmtx);
		DELAY(4);
		uart_lock(hwmtx);
	}

	rdptr = SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_in_rdptr));
	c = SBBC_SRAM_READ_1(sbbc_solcons + rdptr);
	uart_barrier(bas);
	if (++rdptr == SBBC_SRAM_READ_4(sbbc_solcons +
	    SBBC_CONS_OFF(cons_in_end)))
		rdptr = SBBC_SRAM_READ_4(sbbc_solcons +
		    SBBC_CONS_OFF(cons_in_begin));
	SBBC_SRAM_WRITE_4(sbbc_solcons + SBBC_CONS_OFF(cons_in_rdptr),
	    rdptr);
	uart_barrier(bas);
	SBBC_SRAM_WRITE_4(sbbc_solscir, SBBC_SRAM_READ_4(sbbc_solscir) |
	    SBBC_SRAM_CONS_SPACE_IN);
	uart_barrier(bas);
	sbbc_send_intr(bst, bsh);

	uart_unlock(hwmtx);
	return (c);
}

/*
 * High-level UART interface
 */
static int sbbc_uart_bus_attach(struct uart_softc *sc);
static int sbbc_uart_bus_detach(struct uart_softc *sc);
static int sbbc_uart_bus_flush(struct uart_softc *sc, int what);
static int sbbc_uart_bus_getsig(struct uart_softc *sc);
static int sbbc_uart_bus_ioctl(struct uart_softc *sc, int request,
    intptr_t data);
static int sbbc_uart_bus_ipend(struct uart_softc *sc);
static int sbbc_uart_bus_param(struct uart_softc *sc, int baudrate,
    int databits, int stopbits, int parity);
static int sbbc_uart_bus_probe(struct uart_softc *sc);
static int sbbc_uart_bus_receive(struct uart_softc *sc);
static int sbbc_uart_bus_setsig(struct uart_softc *sc, int sig);
static int sbbc_uart_bus_transmit(struct uart_softc *sc);

static kobj_method_t sbbc_uart_methods[] = {
	KOBJMETHOD(uart_attach,		sbbc_uart_bus_attach),
	KOBJMETHOD(uart_detach,		sbbc_uart_bus_detach),
	KOBJMETHOD(uart_flush,		sbbc_uart_bus_flush),
	KOBJMETHOD(uart_getsig,		sbbc_uart_bus_getsig),
	KOBJMETHOD(uart_ioctl,		sbbc_uart_bus_ioctl),
	KOBJMETHOD(uart_ipend,		sbbc_uart_bus_ipend),
	KOBJMETHOD(uart_param,		sbbc_uart_bus_param),
	KOBJMETHOD(uart_probe,		sbbc_uart_bus_probe),
	KOBJMETHOD(uart_receive,	sbbc_uart_bus_receive),
	KOBJMETHOD(uart_setsig,		sbbc_uart_bus_setsig),
	KOBJMETHOD(uart_transmit,	sbbc_uart_bus_transmit),

	DEVMETHOD_END
};

struct uart_class uart_sbbc_class = {
	"sbbc",
	sbbc_uart_methods,
	sizeof(struct uart_softc),
	.uc_ops = &sbbc_uart_ops,
	.uc_range = 1,
	.uc_rclk = 0x5bbc,	/* arbitrary */
	.uc_rshift = 0
};

#define	SIGCHG(c, i, s, d)						\
	if ((c) != 0) {							\
		i |= (((i) & (s)) != 0) ? (s) : (s) | (d);		\
	} else {							\
		i = (((i) & (s)) != 0) ? ((i) & ~(s)) | (d) : (i);	\
	}

static int
sbbc_uart_bus_attach(struct uart_softc *sc)
{
	struct uart_bas *bas;
	bus_space_tag_t bst;
	bus_space_handle_t bsh;
	uint32_t wrptr;

	bas = &sc->sc_bas;
	bst = bas->bst;
	bsh = bas->bsh;

	uart_lock(sc->sc_hwmtx);

	/*
	 * Let the current output drain before enabling interrupts.  Not
	 * doing so tends to cause lost output when turning them on.
	 */
	wrptr = SBBC_SRAM_READ_4(sbbc_solcons +
	    SBBC_CONS_OFF(cons_out_wrptr));
	while (SBBC_SRAM_READ_4(sbbc_solcons +
	    SBBC_CONS_OFF(cons_out_rdptr)) != wrptr);
		cpu_spinwait();

	/* Clear and acknowledge possibly outstanding interrupts. */
	SBBC_SRAM_WRITE_4(sbbc_scsolir, 0);
	uart_barrier(bas);
	SBBC_REGS_WRITE_4(SBBC_PCI_INT_STATUS,
	    SBBC_SRAM_READ_4(sbbc_scsolir));
	uart_barrier(bas);
	/* Enable PCI interrupts. */
	SBBC_REGS_WRITE_4(SBBC_PCI_INT_ENABLE, SBBC_PCI_ENABLE_INT_A);
	uart_barrier(bas);
	/* Enable input from and output to SC as well as break interrupts. */
	SBBC_SRAM_WRITE_4(sbbc_scsolie, SBBC_SRAM_READ_4(sbbc_scsolie) |
	    SBBC_SRAM_CONS_IN | SBBC_SRAM_CONS_BRK |
	    SBBC_SRAM_CONS_SPACE_OUT);
	uart_barrier(bas);

	uart_unlock(sc->sc_hwmtx);
	return (0);
}

static int
sbbc_uart_bus_detach(struct uart_softc *sc)
{

	/* Give back the console input. */
	sbbc_serengeti_set_console_input(SUNW_SETCONSINPUT_OBP);
	return (0);
}

static int
sbbc_uart_bus_flush(struct uart_softc *sc, int what)
{
	struct uart_bas *bas;
	bus_space_tag_t bst;
	bus_space_handle_t bsh;

	bas = &sc->sc_bas;
	bst = bas->bst;
	bsh = bas->bsh;

	if ((what & UART_FLUSH_TRANSMITTER) != 0)
		return (ENODEV);
	if ((what & UART_FLUSH_RECEIVER) != 0) {
		SBBC_SRAM_WRITE_4(sbbc_solcons +
		    SBBC_CONS_OFF(cons_in_rdptr),
		    SBBC_SRAM_READ_4(sbbc_solcons +
		    SBBC_CONS_OFF(cons_in_wrptr)));
		uart_barrier(bas);
	}
	return (0);
}

static int
sbbc_uart_bus_getsig(struct uart_softc *sc)
{
	uint32_t dummy, new, old, sig;

	do {
		old = sc->sc_hwsig;
		sig = old;
		dummy = 0;
		SIGCHG(dummy, sig, SER_CTS, SER_DCTS);
		SIGCHG(dummy, sig, SER_DCD, SER_DDCD);
		SIGCHG(dummy, sig, SER_DSR, SER_DDSR);
		new = sig & ~SER_MASK_DELTA;
	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
	return (sig);
}

static int
sbbc_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
{
	int error;

	error = 0;
	uart_lock(sc->sc_hwmtx);
	switch (request) {
	case UART_IOCTL_BAUD:
		*(int*)data = 9600;	/* arbitrary */
		break;
	default:
		error = EINVAL;
		break;
	}
	uart_unlock(sc->sc_hwmtx);
	return (error);
}

static int
sbbc_uart_bus_ipend(struct uart_softc *sc)
{
	struct uart_bas *bas;
	bus_space_tag_t bst;
	bus_space_handle_t bsh;
	int ipend;
	uint32_t reason, status;

	bas = &sc->sc_bas;
	bst = bas->bst;
	bsh = bas->bsh;

	uart_lock(sc->sc_hwmtx);
	status = SBBC_REGS_READ_4(SBBC_PCI_INT_STATUS);
	if (status == 0) {
		uart_unlock(sc->sc_hwmtx);
		return (0);
	}

	/*
	 * Unfortunately, we can't use compare and swap for non-cachable
	 * memory.
	 */
	reason = SBBC_SRAM_READ_4(sbbc_scsolir);
	SBBC_SRAM_WRITE_4(sbbc_scsolir, 0);
	uart_barrier(bas);
	/* Acknowledge the interrupt. */
	SBBC_REGS_WRITE_4(SBBC_PCI_INT_STATUS, status);
	uart_barrier(bas);

	uart_unlock(sc->sc_hwmtx);

	ipend = 0;
	if ((reason & SBBC_SRAM_CONS_IN) != 0)
		ipend |= SER_INT_RXREADY;
	if ((reason & SBBC_SRAM_CONS_BRK) != 0)
		ipend |= SER_INT_BREAK;
	if ((reason & SBBC_SRAM_CONS_SPACE_OUT) != 0 &&
	    SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_out_rdptr)) ==
	    SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_out_wrptr)))
		ipend |= SER_INT_TXIDLE;
	return (ipend);
}

static int
sbbc_uart_bus_param(struct uart_softc *sc __unused, int baudrate __unused,
    int databits __unused, int stopbits __unused, int parity __unused)
{

	return (0);
}

static int
sbbc_uart_bus_probe(struct uart_softc *sc)
{
	struct uart_bas *bas;
	bus_space_tag_t bst;
	bus_space_handle_t bsh;

	if (sbbc_console != 0) {
		bas = &sc->sc_bas;
		bst = bas->bst;
		bsh = bas->bsh;
		sc->sc_rxfifosz = SBBC_SRAM_READ_4(sbbc_solcons +
		    SBBC_CONS_OFF(cons_in_end)) - SBBC_SRAM_READ_4(sbbc_solcons +
		    SBBC_CONS_OFF(cons_in_begin)) - 1;
		sc->sc_txfifosz = SBBC_SRAM_READ_4(sbbc_solcons +
		    SBBC_CONS_OFF(cons_out_end)) - SBBC_SRAM_READ_4(sbbc_solcons +
		    SBBC_CONS_OFF(cons_out_begin)) - 1;
		return (0);
	}
	return (ENXIO);
}

static int
sbbc_uart_bus_receive(struct uart_softc *sc)
{
	struct uart_bas *bas;
	bus_space_tag_t bst;
	bus_space_handle_t bsh;
	int c;
	uint32_t end, rdptr, wrptr;

	bas = &sc->sc_bas;
	bst = bas->bst;
	bsh = bas->bsh;

	uart_lock(sc->sc_hwmtx);

	end = SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_in_end));
	rdptr = SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_in_rdptr));
	wrptr = SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_in_wrptr));
	while (rdptr != wrptr) {
		if (uart_rx_full(sc) != 0) {
			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
			break;
		}
		c = SBBC_SRAM_READ_1(sbbc_solcons + rdptr);
		uart_rx_put(sc, c);
		if (++rdptr == end)
			rdptr = SBBC_SRAM_READ_4(sbbc_solcons +
			    SBBC_CONS_OFF(cons_in_begin));
	}
	uart_barrier(bas);
	SBBC_SRAM_WRITE_4(sbbc_solcons + SBBC_CONS_OFF(cons_in_rdptr),
	    rdptr);
	uart_barrier(bas);
	SBBC_SRAM_WRITE_4(sbbc_solscir, SBBC_SRAM_READ_4(sbbc_solscir) |
	    SBBC_SRAM_CONS_SPACE_IN);
	uart_barrier(bas);
	sbbc_send_intr(bst, bsh);

	uart_unlock(sc->sc_hwmtx);
	return (0);
}

static int
sbbc_uart_bus_setsig(struct uart_softc *sc, int sig)
{
	struct uart_bas *bas;
	uint32_t new, old;

	bas = &sc->sc_bas;
	do {
		old = sc->sc_hwsig;
		new = old;
		if ((sig & SER_DDTR) != 0) {
			SIGCHG(sig & SER_DTR, new, SER_DTR, SER_DDTR);
		}
		if ((sig & SER_DRTS) != 0) {
			SIGCHG(sig & SER_RTS, new, SER_RTS, SER_DRTS);
		}
	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
	return (0);
}

static int
sbbc_uart_bus_transmit(struct uart_softc *sc)
{
	struct uart_bas *bas;
	bus_space_tag_t bst;
	bus_space_handle_t bsh;
	int i;
	uint32_t end, wrptr;

	bas = &sc->sc_bas;
	bst = bas->bst;
	bsh = bas->bsh;

	uart_lock(sc->sc_hwmtx);

	end = SBBC_SRAM_READ_4(sbbc_solcons + SBBC_CONS_OFF(cons_out_end));
	wrptr = SBBC_SRAM_READ_4(sbbc_solcons +
	    SBBC_CONS_OFF(cons_out_wrptr));
	for (i = 0; i < sc->sc_txdatasz; i++) {
		SBBC_SRAM_WRITE_1(sbbc_solcons + wrptr, sc->sc_txbuf[i]);
		if (++wrptr == end)
			wrptr = SBBC_SRAM_READ_4(sbbc_solcons +
			    SBBC_CONS_OFF(cons_out_begin));
	}
	uart_barrier(bas);
	SBBC_SRAM_WRITE_4(sbbc_solcons + SBBC_CONS_OFF(cons_out_wrptr),
	    wrptr);
	uart_barrier(bas);
	SBBC_SRAM_WRITE_4(sbbc_solscir, SBBC_SRAM_READ_4(sbbc_solscir) |
	    SBBC_SRAM_CONS_OUT);
	uart_barrier(bas);
	sbbc_send_intr(bst, bsh);
	sc->sc_txbusy = 1;

	uart_unlock(sc->sc_hwmtx);
	return (0);
}
OpenPOWER on IntegriCloud