1/*
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright 2003-2011 Netlogic Microsystems (Netlogic). All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in
15 *    the documentation and/or other materials provided with the
16 *    distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY Netlogic Microsystems ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * NETLOGIC_BSD */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/bus.h>
38#include <sys/kernel.h>
39#include <sys/endian.h>
40#include <sys/malloc.h>
41#include <sys/ktr.h>
42
43#include <vm/vm.h>
44#include <vm/pmap.h>
45#include <vm/vm_kern.h>
46#include <vm/vm_extern.h>
47
48#include <machine/bus.h>
49#include <machine/cache.h>
50
51static int
52rmi_bus_space_map(void *t, bus_addr_t addr,
53    bus_size_t size, int flags,
54    bus_space_handle_t *bshp);
55
56static void
57rmi_bus_space_unmap(void *t, bus_space_handle_t bsh,
58    bus_size_t size);
59
60static int
61rmi_bus_space_subregion(void *t,
62    bus_space_handle_t bsh,
63    bus_size_t offset, bus_size_t size,
64    bus_space_handle_t *nbshp);
65
66static u_int8_t
67rmi_bus_space_read_1(void *t,
68    bus_space_handle_t handle,
69    bus_size_t offset);
70
71static u_int16_t
72rmi_bus_space_read_2(void *t,
73    bus_space_handle_t handle,
74    bus_size_t offset);
75
76static u_int32_t
77rmi_bus_space_read_4(void *t,
78    bus_space_handle_t handle,
79    bus_size_t offset);
80
81static void
82rmi_bus_space_read_multi_1(void *t,
83    bus_space_handle_t handle,
84    bus_size_t offset, u_int8_t *addr,
85    size_t count);
86
87static void
88rmi_bus_space_read_multi_2(void *t,
89    bus_space_handle_t handle,
90    bus_size_t offset, u_int16_t *addr,
91    size_t count);
92
93static void
94rmi_bus_space_read_multi_4(void *t,
95    bus_space_handle_t handle,
96    bus_size_t offset, u_int32_t *addr,
97    size_t count);
98
99static void
100rmi_bus_space_read_region_1(void *t,
101    bus_space_handle_t bsh,
102    bus_size_t offset, u_int8_t *addr,
103    size_t count);
104
105static void
106rmi_bus_space_read_region_2(void *t,
107    bus_space_handle_t bsh,
108    bus_size_t offset, u_int16_t *addr,
109    size_t count);
110
111static void
112rmi_bus_space_read_region_4(void *t,
113    bus_space_handle_t bsh,
114    bus_size_t offset, u_int32_t *addr,
115    size_t count);
116
117static void
118rmi_bus_space_write_1(void *t,
119    bus_space_handle_t handle,
120    bus_size_t offset, u_int8_t value);
121
122static void
123rmi_bus_space_write_2(void *t,
124    bus_space_handle_t handle,
125    bus_size_t offset, u_int16_t value);
126
127static void
128rmi_bus_space_write_4(void *t,
129    bus_space_handle_t handle,
130    bus_size_t offset, u_int32_t value);
131
132static void
133rmi_bus_space_write_multi_1(void *t,
134    bus_space_handle_t handle,
135    bus_size_t offset,
136    const u_int8_t *addr,
137    size_t count);
138
139static void
140rmi_bus_space_write_multi_2(void *t,
141    bus_space_handle_t handle,
142    bus_size_t offset,
143    const u_int16_t *addr,
144    size_t count);
145
146static void
147rmi_bus_space_write_multi_4(void *t,
148    bus_space_handle_t handle,
149    bus_size_t offset,
150    const u_int32_t *addr,
151    size_t count);
152
153static void
154rmi_bus_space_write_region_2(void *t,
155    bus_space_handle_t bsh,
156    bus_size_t offset,
157    const u_int16_t *addr,
158    size_t count);
159
160static void
161rmi_bus_space_write_region_4(void *t,
162    bus_space_handle_t bsh,
163    bus_size_t offset,
164    const u_int32_t *addr,
165    size_t count);
166
167static void
168rmi_bus_space_set_region_2(void *t,
169    bus_space_handle_t bsh,
170    bus_size_t offset, u_int16_t value,
171    size_t count);
172static void
173rmi_bus_space_set_region_4(void *t,
174    bus_space_handle_t bsh,
175    bus_size_t offset, u_int32_t value,
176    size_t count);
177
178static void
179rmi_bus_space_barrier(void *tag __unused, bus_space_handle_t bsh __unused,
180    bus_size_t offset __unused, bus_size_t len __unused, int flags);
181
182static void
183rmi_bus_space_copy_region_2(void *t,
184    bus_space_handle_t bsh1,
185    bus_size_t off1,
186    bus_space_handle_t bsh2,
187    bus_size_t off2, size_t count);
188
189u_int8_t
190rmi_bus_space_read_stream_1(void *t, bus_space_handle_t handle,
191    bus_size_t offset);
192
193static u_int16_t
194rmi_bus_space_read_stream_2(void *t, bus_space_handle_t handle,
195    bus_size_t offset);
196
197static u_int32_t
198rmi_bus_space_read_stream_4(void *t, bus_space_handle_t handle,
199    bus_size_t offset);
200static void
201rmi_bus_space_read_multi_stream_1(void *t,
202    bus_space_handle_t handle,
203    bus_size_t offset, u_int8_t *addr,
204    size_t count);
205
206static void
207rmi_bus_space_read_multi_stream_2(void *t,
208    bus_space_handle_t handle,
209    bus_size_t offset, u_int16_t *addr,
210    size_t count);
211
212static void
213rmi_bus_space_read_multi_stream_4(void *t,
214    bus_space_handle_t handle,
215    bus_size_t offset, u_int32_t *addr,
216    size_t count);
217
218void
219rmi_bus_space_write_stream_1(void *t, bus_space_handle_t bsh,
220    bus_size_t offset, u_int8_t value);
221static void
222rmi_bus_space_write_stream_2(void *t, bus_space_handle_t handle,
223    bus_size_t offset, u_int16_t value);
224
225static void
226rmi_bus_space_write_stream_4(void *t, bus_space_handle_t handle,
227    bus_size_t offset, u_int32_t value);
228
229static void
230rmi_bus_space_write_multi_stream_1(void *t,
231    bus_space_handle_t handle,
232    bus_size_t offset,
233    const u_int8_t *addr,
234    size_t count);
235static void
236rmi_bus_space_write_multi_stream_2(void *t,
237    bus_space_handle_t handle,
238    bus_size_t offset,
239    const u_int16_t *addr,
240    size_t count);
241
242static void
243rmi_bus_space_write_multi_stream_4(void *t,
244    bus_space_handle_t handle,
245    bus_size_t offset,
246    const u_int32_t *addr,
247    size_t count);
248
249#define TODO() printf("XLP bus space: '%s' unimplemented\n", __func__)
250
251static struct bus_space local_rmi_bus_space = {
252	/* cookie */
253	(void *)0,
254
255	/* mapping/unmapping */
256	rmi_bus_space_map,
257	rmi_bus_space_unmap,
258	rmi_bus_space_subregion,
259
260	/* allocation/deallocation */
261	NULL,
262	NULL,
263
264	/* barrier */
265	rmi_bus_space_barrier,
266
267	/* read (single) */
268	rmi_bus_space_read_1,
269	rmi_bus_space_read_2,
270	rmi_bus_space_read_4,
271	NULL,
272
273	/* read multiple */
274	rmi_bus_space_read_multi_1,
275	rmi_bus_space_read_multi_2,
276	rmi_bus_space_read_multi_4,
277	NULL,
278
279	/* read region */
280	rmi_bus_space_read_region_1,
281	rmi_bus_space_read_region_2,
282	rmi_bus_space_read_region_4,
283	NULL,
284
285	/* write (single) */
286	rmi_bus_space_write_1,
287	rmi_bus_space_write_2,
288	rmi_bus_space_write_4,
289	NULL,
290
291	/* write multiple */
292	rmi_bus_space_write_multi_1,
293	rmi_bus_space_write_multi_2,
294	rmi_bus_space_write_multi_4,
295	NULL,
296
297	/* write region */
298	NULL,
299	rmi_bus_space_write_region_2,
300	rmi_bus_space_write_region_4,
301	NULL,
302
303	/* set multiple */
304	NULL,
305	NULL,
306	NULL,
307	NULL,
308
309	/* set region */
310	NULL,
311	rmi_bus_space_set_region_2,
312	rmi_bus_space_set_region_4,
313	NULL,
314
315	/* copy */
316	NULL,
317	rmi_bus_space_copy_region_2,
318	NULL,
319	NULL,
320
321	/* read (single) stream */
322	rmi_bus_space_read_stream_1,
323	rmi_bus_space_read_stream_2,
324	rmi_bus_space_read_stream_4,
325	NULL,
326
327	/* read multiple stream */
328	rmi_bus_space_read_multi_stream_1,
329	rmi_bus_space_read_multi_stream_2,
330	rmi_bus_space_read_multi_stream_4,
331	NULL,
332
333	/* read region stream */
334	rmi_bus_space_read_region_1,
335	rmi_bus_space_read_region_2,
336	rmi_bus_space_read_region_4,
337	NULL,
338
339	/* write (single) stream */
340	rmi_bus_space_write_stream_1,
341	rmi_bus_space_write_stream_2,
342	rmi_bus_space_write_stream_4,
343	NULL,
344
345	/* write multiple stream */
346	rmi_bus_space_write_multi_stream_1,
347	rmi_bus_space_write_multi_stream_2,
348	rmi_bus_space_write_multi_stream_4,
349	NULL,
350
351	/* write region stream */
352	NULL,
353	rmi_bus_space_write_region_2,
354	rmi_bus_space_write_region_4,
355	NULL,
356};
357
358/* generic bus_space tag */
359bus_space_tag_t rmi_bus_space = &local_rmi_bus_space;
360
361/*
362 * Map a region of device bus space into CPU virtual address space.
363 */
364static int
365rmi_bus_space_map(void *t __unused, bus_addr_t addr,
366    bus_size_t size __unused, int flags __unused,
367    bus_space_handle_t *bshp)
368{
369
370	*bshp = MIPS_PHYS_TO_DIRECT_UNCACHED(addr);
371	return (0);
372}
373
374/*
375 * Unmap a region of device bus space.
376 */
377static void
378rmi_bus_space_unmap(void *t __unused, bus_space_handle_t bsh __unused,
379    bus_size_t size __unused)
380{
381}
382
383/*
384 * Get a new handle for a subregion of an already-mapped area of bus space.
385 */
386
387static int
388rmi_bus_space_subregion(void *t __unused, bus_space_handle_t bsh,
389    bus_size_t offset, bus_size_t size __unused,
390    bus_space_handle_t *nbshp)
391{
392	*nbshp = bsh + offset;
393	return (0);
394}
395
396/*
397 * Read a 1, 2, 4, or 8 byte quantity from bus space
398 * described by tag/handle/offset.
399 */
400
401static u_int8_t
402rmi_bus_space_read_1(void *tag, bus_space_handle_t handle,
403    bus_size_t offset)
404{
405	return (u_int8_t) (*(volatile u_int8_t *)(handle + offset));
406}
407
408static u_int16_t
409rmi_bus_space_read_2(void *tag, bus_space_handle_t handle,
410    bus_size_t offset)
411{
412	return (u_int16_t)(*(volatile u_int16_t *)(handle + offset));
413}
414
415static u_int32_t
416rmi_bus_space_read_4(void *tag, bus_space_handle_t handle,
417    bus_size_t offset)
418{
419	return (*(volatile u_int32_t *)(handle + offset));
420}
421
422/*
423 * Read `count' 1, 2, 4, or 8 byte quantities from bus space
424 * described by tag/handle/offset and copy into buffer provided.
425 */
426static void
427rmi_bus_space_read_multi_1(void *tag, bus_space_handle_t handle,
428    bus_size_t offset, u_int8_t *addr, size_t count)
429{
430	TODO();
431}
432
433static void
434rmi_bus_space_read_multi_2(void *tag, bus_space_handle_t handle,
435    bus_size_t offset, u_int16_t *addr, size_t count)
436{
437	TODO();
438}
439
440static void
441rmi_bus_space_read_multi_4(void *tag, bus_space_handle_t handle,
442    bus_size_t offset, u_int32_t *addr, size_t count)
443{
444	TODO();
445}
446
447/*
448 * Write the 1, 2, 4, or 8 byte value `value' to bus space
449 * described by tag/handle/offset.
450 */
451
452static void
453rmi_bus_space_write_1(void *tag, bus_space_handle_t handle,
454    bus_size_t offset, u_int8_t value)
455{
456	*(volatile u_int8_t *)(handle + offset) =  value;
457}
458
459static void
460rmi_bus_space_write_2(void *tag, bus_space_handle_t handle,
461    bus_size_t offset, u_int16_t value)
462{
463	*(volatile u_int16_t *)(handle + offset) = value;
464}
465
466static void
467rmi_bus_space_write_4(void *tag, bus_space_handle_t handle,
468    bus_size_t offset, u_int32_t value)
469{
470	*(volatile u_int32_t *)(handle + offset) = value;
471}
472
473/*
474 * Write `count' 1, 2, 4, or 8 byte quantities from the buffer
475 * provided to bus space described by tag/handle/offset.
476 */
477
478static void
479rmi_bus_space_write_multi_1(void *tag, bus_space_handle_t handle,
480    bus_size_t offset, const u_int8_t *addr, size_t count)
481{
482	TODO();
483}
484
485static void
486rmi_bus_space_write_multi_2(void *tag, bus_space_handle_t handle,
487    bus_size_t offset, const u_int16_t *addr, size_t count)
488{
489	TODO();
490}
491
492static void
493rmi_bus_space_write_multi_4(void *tag, bus_space_handle_t handle,
494    bus_size_t offset, const u_int32_t *addr, size_t count)
495{
496	TODO();
497}
498
499/*
500 * Write `count' 1, 2, 4, or 8 byte value `val' to bus space described
501 * by tag/handle starting at `offset'.
502 */
503
504static void
505rmi_bus_space_set_region_2(void *t, bus_space_handle_t bsh,
506    bus_size_t offset, u_int16_t value, size_t count)
507{
508	bus_addr_t addr = bsh + offset;
509
510	for (; count != 0; count--, addr += 2)
511		(*(volatile u_int32_t *)(addr)) = value;
512}
513
514static void
515rmi_bus_space_set_region_4(void *t, bus_space_handle_t bsh,
516    bus_size_t offset, u_int32_t value, size_t count)
517{
518	bus_addr_t addr = bsh + offset;
519
520	for (; count != 0; count--, addr += 4)
521		(*(volatile u_int32_t *)(addr)) = value;
522}
523
524/*
525 * Copy `count' 1, 2, 4, or 8 byte values from bus space starting
526 * at tag/bsh1/off1 to bus space starting at tag/bsh2/off2.
527 */
528static void
529rmi_bus_space_copy_region_2(void *t, bus_space_handle_t bsh1,
530    bus_size_t off1, bus_space_handle_t bsh2,
531    bus_size_t off2, size_t count)
532{
533	printf("bus_space_copy_region_2 - unimplemented\n");
534}
535
536/*
537 * Read `count' 1, 2, 4, or 8 byte quantities from bus space
538 * described by tag/handle/offset and copy into buffer provided.
539 */
540
541u_int8_t
542rmi_bus_space_read_stream_1(void *t, bus_space_handle_t handle,
543    bus_size_t offset)
544{
545
546	return *((volatile u_int8_t *)(handle + offset));
547}
548
549static u_int16_t
550rmi_bus_space_read_stream_2(void *t, bus_space_handle_t handle,
551    bus_size_t offset)
552{
553	return *(volatile u_int16_t *)(handle + offset);
554}
555
556static u_int32_t
557rmi_bus_space_read_stream_4(void *t, bus_space_handle_t handle,
558    bus_size_t offset)
559{
560	return (*(volatile u_int32_t *)(handle + offset));
561}
562
563static void
564rmi_bus_space_read_multi_stream_1(void *tag, bus_space_handle_t handle,
565    bus_size_t offset, u_int8_t *addr, size_t count)
566{
567	TODO();
568}
569
570static void
571rmi_bus_space_read_multi_stream_2(void *tag, bus_space_handle_t handle,
572    bus_size_t offset, u_int16_t *addr, size_t count)
573{
574	TODO();
575}
576
577static void
578rmi_bus_space_read_multi_stream_4(void *tag, bus_space_handle_t handle,
579    bus_size_t offset, u_int32_t *addr, size_t count)
580{
581	TODO();
582}
583
584/*
585 * Read `count' 1, 2, 4, or 8 byte quantities from bus space
586 * described by tag/handle and starting at `offset' and copy into
587 * buffer provided.
588 */
589void
590rmi_bus_space_read_region_1(void *t, bus_space_handle_t bsh,
591    bus_size_t offset, u_int8_t *addr, size_t count)
592{
593	TODO();
594}
595
596void
597rmi_bus_space_read_region_2(void *t, bus_space_handle_t bsh,
598    bus_size_t offset, u_int16_t *addr, size_t count)
599{
600	TODO();
601}
602
603void
604rmi_bus_space_read_region_4(void *t, bus_space_handle_t bsh,
605    bus_size_t offset, u_int32_t *addr, size_t count)
606{
607	bus_addr_t baddr = bsh + offset;
608
609	while (count--) {
610		*addr++ = (*(volatile u_int32_t *)(baddr));
611		baddr += 4;
612	}
613}
614
615void
616rmi_bus_space_write_stream_1(void *t, bus_space_handle_t handle,
617    bus_size_t offset, u_int8_t value)
618{
619	TODO();
620}
621
622static void
623rmi_bus_space_write_stream_2(void *t, bus_space_handle_t handle,
624    bus_size_t offset, u_int16_t value)
625{
626	TODO();
627}
628
629static void
630rmi_bus_space_write_stream_4(void *t, bus_space_handle_t handle,
631    bus_size_t offset, u_int32_t value)
632{
633	TODO();
634}
635
636static void
637rmi_bus_space_write_multi_stream_1(void *tag, bus_space_handle_t handle,
638    bus_size_t offset, const u_int8_t *addr, size_t count)
639{
640	TODO();
641}
642
643static void
644rmi_bus_space_write_multi_stream_2(void *tag, bus_space_handle_t handle,
645    bus_size_t offset, const u_int16_t *addr, size_t count)
646{
647	TODO();
648}
649
650static void
651rmi_bus_space_write_multi_stream_4(void *tag, bus_space_handle_t handle,
652    bus_size_t offset, const u_int32_t *addr, size_t count)
653{
654	TODO();
655}
656
657void
658rmi_bus_space_write_region_2(void *t,
659    bus_space_handle_t bsh,
660    bus_size_t offset,
661    const u_int16_t *addr,
662    size_t count)
663{
664	TODO();
665}
666
667void
668rmi_bus_space_write_region_4(void *t, bus_space_handle_t bsh,
669    bus_size_t offset, const u_int32_t *addr, size_t count)
670{
671	TODO();
672}
673
674static void
675rmi_bus_space_barrier(void *tag __unused, bus_space_handle_t bsh __unused,
676    bus_size_t offset __unused, bus_size_t len __unused, int flags)
677{
678}
679
680/*
681 * need a special bus space for this, because the Netlogic SoC
682 * UART allows only 32 bit access to its registers
683 */
684
685static u_int8_t
686rmi_uart_bus_space_read_1(void *tag, bus_space_handle_t handle,
687    bus_size_t offset)
688{
689	return (u_int8_t)(*(volatile u_int32_t *)(handle + offset));
690}
691
692static void
693rmi_uart_bus_space_write_1(void *tag, bus_space_handle_t handle,
694    bus_size_t offset, u_int8_t value)
695{
696	*(volatile u_int32_t *)(handle + offset) =  value;
697}
698
699static struct bus_space local_rmi_uart_bus_space = {
700	/* cookie */
701	(void *)0,
702
703	/* mapping/unmapping */
704	rmi_bus_space_map,
705	rmi_bus_space_unmap,
706	rmi_bus_space_subregion,
707
708	/* allocation/deallocation */
709	NULL,
710	NULL,
711
712	/* barrier */
713	rmi_bus_space_barrier,
714
715	/* read (single) */
716	rmi_uart_bus_space_read_1, NULL, NULL, NULL,
717
718	/* read multiple */
719	NULL, NULL, NULL, NULL,
720
721	/* read region */
722	NULL, NULL, NULL, NULL,
723
724	/* write (single) */
725	rmi_uart_bus_space_write_1, NULL, NULL, NULL,
726
727	/* write multiple */
728	NULL, NULL, NULL, NULL,
729
730	/* write region */
731	NULL, NULL, NULL, NULL,
732
733	/* set multiple */
734	NULL, NULL, NULL, NULL,
735
736	/* set region */
737	NULL, NULL, NULL, NULL,
738
739	/* copy */
740	NULL, NULL, NULL, NULL,
741
742	/* read (single) stream */
743	NULL, NULL, NULL, NULL,
744
745	/* read multiple stream */
746	NULL, NULL, NULL, NULL,
747
748	/* read region stream */
749	NULL, NULL, NULL, NULL,
750
751	/* write (single) stream */
752	NULL, NULL, NULL, NULL,
753
754	/* write multiple stream */
755	NULL, NULL, NULL, NULL,
756
757	/* write region stream */
758	NULL, NULL, NULL, NULL,
759};
760
761/* generic bus_space tag */
762bus_space_tag_t rmi_uart_bus_space = &local_rmi_uart_bus_space;
763