1 /*      $NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $    */
2 /*-
3  * $Id: bus.h,v 1.6 2007/08/09 11:23:32 katta Exp $
4  *
5  * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-4-Clause
6  *
7  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
12  * NASA Ames Research Center.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
38  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *      This product includes software developed by Christopher G. Demetriou
51  *	for the NetBSD Project.
52  * 4. The name of the author may not be used to endorse or promote products
53  *    derived from this software without specific prior written permission
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
56  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
58  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
59  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
60  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
64  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  *
66  *	from: src/sys/alpha/include/bus.h,v 1.5 1999/08/28 00:38:40 peter
67  * $FreeBSD$
68  */
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/bus.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
77 #include <sys/ktr.h>
78 #include <sys/endian.h>
79 
80 #include <vm/vm.h>
81 #include <vm/pmap.h>
82 #include <vm/vm_kern.h>
83 #include <vm/vm_extern.h>
84 
85 #include <machine/bus.h>
86 #include <machine/cache.h>
87 
88 #include <mips/cavium/octopcivar.h>
89 
90 #include <contrib/octeon-sdk/cvmx.h>
91 
92 static struct bus_space octopci_space = {
93 	/* cookie */
94 	(void *) 0,
95 
96 	/* mapping/unmapping */
97 	octopci_bs_map,
98 	octopci_bs_unmap,
99 	octopci_bs_subregion,
100 
101 	/* allocation/deallocation */
102 	NULL,
103 	NULL,
104 
105 	/* barrier */
106 	octopci_bs_barrier,
107 
108 	/* read (single) */
109 	octopci_bs_r_1,
110 	octopci_bs_r_2,
111 	octopci_bs_r_4,
112 	NULL,
113 
114 	/* read multiple */
115 	octopci_bs_rm_1,
116 	octopci_bs_rm_2,
117 	octopci_bs_rm_4,
118 	NULL,
119 
120 	/* read region */
121 	octopci_bs_rr_1,
122 	octopci_bs_rr_2,
123 	octopci_bs_rr_4,
124 	NULL,
125 
126 	/* write (single) */
127 	octopci_bs_w_1,
128 	octopci_bs_w_2,
129 	octopci_bs_w_4,
130 	NULL,
131 
132 	/* write multiple */
133 	octopci_bs_wm_1,
134 	octopci_bs_wm_2,
135 	octopci_bs_wm_4,
136 	NULL,
137 
138 	/* write region */
139 	NULL,
140 	octopci_bs_wr_2,
141 	octopci_bs_wr_4,
142 	NULL,
143 
144 	/* set multiple */
145 	NULL,
146 	NULL,
147 	NULL,
148 	NULL,
149 
150 	/* set region */
151 	NULL,
152 	octopci_bs_sr_2,
153 	octopci_bs_sr_4,
154 	NULL,
155 
156 	/* copy */
157 	NULL,
158 	octopci_bs_c_2,
159 	NULL,
160 	NULL,
161 
162 	/* read (single) stream */
163 	octopci_bs_r_1,
164 	octopci_bs_r_2,
165 	octopci_bs_r_4,
166 	NULL,
167 
168 	/* read multiple stream */
169 	octopci_bs_rm_1,
170 	octopci_bs_rm_2,
171 	octopci_bs_rm_4,
172 	NULL,
173 
174 	/* read region stream */
175 	octopci_bs_rr_1,
176 	octopci_bs_rr_2,
177 	octopci_bs_rr_4,
178 	NULL,
179 
180 	/* write (single) stream */
181 	octopci_bs_w_1,
182 	octopci_bs_w_2,
183 	octopci_bs_w_4,
184 	NULL,
185 
186 	/* write multiple stream */
187 	octopci_bs_wm_1,
188 	octopci_bs_wm_2,
189 	octopci_bs_wm_4,
190 	NULL,
191 
192 	/* write region stream */
193 	NULL,
194 	octopci_bs_wr_2,
195 	octopci_bs_wr_4,
196 	NULL,
197 };
198 
199 #define rd8(a)		cvmx_read64_uint8(a)
200 #define rd16(a)		le16toh(cvmx_read64_uint16(a))
201 #define rd32(a)		le32toh(cvmx_read64_uint32(a))
202 #define wr8(a, v)	cvmx_write64_uint8(a, v)
203 #define wr16(a, v)	cvmx_write64_uint16(a, htole16(v))
204 #define wr32(a, v)	cvmx_write64_uint32(a, htole32(v))
205 
206 /* octopci bus_space tag */
207 bus_space_tag_t octopci_bus_space = &octopci_space;
208 
209 int
octopci_bs_map(void * t __unused,bus_addr_t addr,bus_size_t size __unused,int flags __unused,bus_space_handle_t * bshp)210 octopci_bs_map(void *t __unused, bus_addr_t addr,
211 	      bus_size_t size __unused, int flags __unused,
212 	      bus_space_handle_t *bshp)
213 {
214 
215 	*bshp = addr;
216 	return (0);
217 }
218 
219 void
octopci_bs_unmap(void * t __unused,bus_space_handle_t bh __unused,bus_size_t size __unused)220 octopci_bs_unmap(void *t __unused, bus_space_handle_t bh __unused,
221 	      bus_size_t size __unused)
222 {
223 
224 	/* Do nothing */
225 }
226 
227 int
octopci_bs_subregion(void * t __unused,bus_space_handle_t handle,bus_size_t offset,bus_size_t size __unused,bus_space_handle_t * bshp)228 octopci_bs_subregion(void *t __unused, bus_space_handle_t handle,
229 	      bus_size_t offset, bus_size_t size __unused,
230 	      bus_space_handle_t *bshp)
231 {
232 
233 	*bshp = handle + offset;
234 	return (0);
235 }
236 
237 uint8_t
octopci_bs_r_1(void * t,bus_space_handle_t handle,bus_size_t offset)238 octopci_bs_r_1(void *t, bus_space_handle_t handle,
239     bus_size_t offset)
240 {
241 
242 	return (rd8(handle + offset));
243 }
244 
245 uint16_t
octopci_bs_r_2(void * t,bus_space_handle_t handle,bus_size_t offset)246 octopci_bs_r_2(void *t, bus_space_handle_t handle,
247     bus_size_t offset)
248 {
249 
250 	return (rd16(handle + offset));
251 }
252 
253 uint32_t
octopci_bs_r_4(void * t,bus_space_handle_t handle,bus_size_t offset)254 octopci_bs_r_4(void *t, bus_space_handle_t handle,
255     bus_size_t offset)
256 {
257 
258 	return (rd32(handle + offset));
259 }
260 
261 void
octopci_bs_rm_1(void * t,bus_space_handle_t bsh,bus_size_t offset,uint8_t * addr,size_t count)262 octopci_bs_rm_1(void *t, bus_space_handle_t bsh,
263     bus_size_t offset, uint8_t *addr, size_t count)
264 {
265 
266 	while (count--)
267 		*addr++ = rd8(bsh + offset);
268 }
269 
270 void
octopci_bs_rm_2(void * t,bus_space_handle_t bsh,bus_size_t offset,uint16_t * addr,size_t count)271 octopci_bs_rm_2(void *t, bus_space_handle_t bsh,
272     bus_size_t offset, uint16_t *addr, size_t count)
273 {
274 	bus_addr_t baddr = bsh + offset;
275 
276 	while (count--)
277 		*addr++ = rd16(baddr);
278 }
279 
280 void
octopci_bs_rm_4(void * t,bus_space_handle_t bsh,bus_size_t offset,uint32_t * addr,size_t count)281 octopci_bs_rm_4(void *t, bus_space_handle_t bsh,
282     bus_size_t offset, uint32_t *addr, size_t count)
283 {
284 	bus_addr_t baddr = bsh + offset;
285 
286 	while (count--)
287 		*addr++ = rd32(baddr);
288 }
289 
290 /*
291  * Read `count' 1, 2, 4, or 8 byte quantities from bus space
292  * described by tag/handle and starting at `offset' and copy into
293  * buffer provided.
294  */
295 void
octopci_bs_rr_1(void * t,bus_space_handle_t bsh,bus_size_t offset,uint8_t * addr,size_t count)296 octopci_bs_rr_1(void *t, bus_space_handle_t bsh,
297     bus_size_t offset, uint8_t *addr, size_t count)
298 {
299 	bus_addr_t baddr = bsh + offset;
300 
301 	while (count--) {
302 		*addr++ = rd8(baddr);
303 		baddr += 1;
304 	}
305 }
306 
307 void
octopci_bs_rr_2(void * t,bus_space_handle_t bsh,bus_size_t offset,uint16_t * addr,size_t count)308 octopci_bs_rr_2(void *t, bus_space_handle_t bsh,
309     bus_size_t offset, uint16_t *addr, size_t count)
310 {
311 	bus_addr_t baddr = bsh + offset;
312 
313 	while (count--) {
314 		*addr++ = rd16(baddr);
315 		baddr += 2;
316 	}
317 }
318 
319 void
octopci_bs_rr_4(void * t,bus_space_handle_t bsh,bus_size_t offset,uint32_t * addr,size_t count)320 octopci_bs_rr_4(void *t, bus_space_handle_t bsh,
321     bus_size_t offset, uint32_t *addr, size_t count)
322 {
323 	bus_addr_t baddr = bsh + offset;
324 
325 	while (count--) {
326 		*addr++ = rd32(baddr);
327 		baddr += 4;
328 	}
329 }
330 
331 /*
332  * Write the 1, 2, 4, or 8 byte value `value' to bus space
333  * described by tag/handle/offset.
334  */
335 void
octopci_bs_w_1(void * t,bus_space_handle_t bsh,bus_size_t offset,uint8_t value)336 octopci_bs_w_1(void *t, bus_space_handle_t bsh,
337     bus_size_t offset, uint8_t value)
338 {
339 
340 	wr8(bsh + offset, value);
341 }
342 
343 void
octopci_bs_w_2(void * t,bus_space_handle_t bsh,bus_size_t offset,uint16_t value)344 octopci_bs_w_2(void *t, bus_space_handle_t bsh,
345     bus_size_t offset, uint16_t value)
346 {
347 
348 	wr16(bsh + offset, value);
349 }
350 
351 void
octopci_bs_w_4(void * t,bus_space_handle_t bsh,bus_size_t offset,uint32_t value)352 octopci_bs_w_4(void *t, bus_space_handle_t bsh,
353     bus_size_t offset, uint32_t value)
354 {
355 
356 	wr32(bsh + offset, value);
357 }
358 
359 /*
360  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer
361  * provided to bus space described by tag/handle/offset.
362  */
363 void
octopci_bs_wm_1(void * t,bus_space_handle_t bsh,bus_size_t offset,const uint8_t * addr,size_t count)364 octopci_bs_wm_1(void *t, bus_space_handle_t bsh,
365     bus_size_t offset, const uint8_t *addr, size_t count)
366 {
367 	bus_addr_t baddr = bsh + offset;
368 
369 	while (count--)
370 		wr8(baddr, *addr++);
371 }
372 
373 void
octopci_bs_wm_2(void * t,bus_space_handle_t bsh,bus_size_t offset,const uint16_t * addr,size_t count)374 octopci_bs_wm_2(void *t, bus_space_handle_t bsh,
375     bus_size_t offset, const uint16_t *addr, size_t count)
376 {
377 	bus_addr_t baddr = bsh + offset;
378 
379 	while (count--)
380 		wr16(baddr, *addr++);
381 }
382 
383 void
octopci_bs_wm_4(void * t,bus_space_handle_t bsh,bus_size_t offset,const uint32_t * addr,size_t count)384 octopci_bs_wm_4(void *t, bus_space_handle_t bsh,
385     bus_size_t offset, const uint32_t *addr, size_t count)
386 {
387 	bus_addr_t baddr = bsh + offset;
388 
389 	while (count--)
390 		wr32(baddr, *addr++);
391 }
392 
393 /*
394  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer provided
395  * to bus space described by tag/handle starting at `offset'.
396  */
397 void
octopci_bs_wr_1(void * t,bus_space_handle_t bsh,bus_size_t offset,const uint8_t * addr,size_t count)398 octopci_bs_wr_1(void *t, bus_space_handle_t bsh,
399     bus_size_t offset, const uint8_t *addr, size_t count)
400 {
401 	bus_addr_t baddr = bsh + offset;
402 
403 	while (count--) {
404 		wr8(baddr, *addr++);
405 		baddr += 1;
406 	}
407 }
408 
409 void
octopci_bs_wr_2(void * t,bus_space_handle_t bsh,bus_size_t offset,const uint16_t * addr,size_t count)410 octopci_bs_wr_2(void *t, bus_space_handle_t bsh,
411     bus_size_t offset, const uint16_t *addr, size_t count)
412 {
413 	bus_addr_t baddr = bsh + offset;
414 
415 	while (count--) {
416 		wr16(baddr, *addr++);
417 		baddr += 2;
418 	}
419 }
420 
421 void
octopci_bs_wr_4(void * t,bus_space_handle_t bsh,bus_size_t offset,const uint32_t * addr,size_t count)422 octopci_bs_wr_4(void *t, bus_space_handle_t bsh,
423     bus_size_t offset, const uint32_t *addr, size_t count)
424 {
425 	bus_addr_t baddr = bsh + offset;
426 
427 	while (count--) {
428 		wr32(baddr, *addr++);
429 		baddr += 4;
430 	}
431 }
432 
433 /*
434  * Write the 1, 2, 4, or 8 byte value `val' to bus space described
435  * by tag/handle/offset `count' times.
436  */
437 void
octopci_bs_sm_1(void * t,bus_space_handle_t bsh,bus_size_t offset,uint8_t value,size_t count)438 octopci_bs_sm_1(void *t, bus_space_handle_t bsh,
439     bus_size_t offset, uint8_t value, size_t count)
440 {
441 	bus_addr_t addr = bsh + offset;
442 
443 	while (count--)
444 		wr8(addr, value);
445 }
446 
447 void
octopci_bs_sm_2(void * t,bus_space_handle_t bsh,bus_size_t offset,uint16_t value,size_t count)448 octopci_bs_sm_2(void *t, bus_space_handle_t bsh,
449     bus_size_t offset, uint16_t value, size_t count)
450 {
451 	bus_addr_t addr = bsh + offset;
452 
453 	while (count--)
454 		wr16(addr, value);
455 }
456 
457 void
octopci_bs_sm_4(void * t,bus_space_handle_t bsh,bus_size_t offset,uint32_t value,size_t count)458 octopci_bs_sm_4(void *t, bus_space_handle_t bsh,
459     bus_size_t offset, uint32_t value, size_t count)
460 {
461 	bus_addr_t addr = bsh + offset;
462 
463 	while (count--)
464 		wr32(addr, value);
465 }
466 
467 /*
468  * Write `count' 1, 2, 4, or 8 byte value `val' to bus space described
469  * by tag/handle starting at `offset'.
470  */
471 void
octopci_bs_sr_1(void * t,bus_space_handle_t bsh,bus_size_t offset,uint8_t value,size_t count)472 octopci_bs_sr_1(void *t, bus_space_handle_t bsh,
473     bus_size_t offset, uint8_t value, size_t count)
474 {
475 	bus_addr_t addr = bsh + offset;
476 
477 	for (; count != 0; count--, addr++)
478 		wr8(addr, value);
479 }
480 
481 void
octopci_bs_sr_2(void * t,bus_space_handle_t bsh,bus_size_t offset,uint16_t value,size_t count)482 octopci_bs_sr_2(void *t, bus_space_handle_t bsh,
483 		       bus_size_t offset, uint16_t value, size_t count)
484 {
485 	bus_addr_t addr = bsh + offset;
486 
487 	for (; count != 0; count--, addr += 2)
488 		wr16(addr, value);
489 }
490 
491 void
octopci_bs_sr_4(void * t,bus_space_handle_t bsh,bus_size_t offset,uint32_t value,size_t count)492 octopci_bs_sr_4(void *t, bus_space_handle_t bsh,
493     bus_size_t offset, uint32_t value, size_t count)
494 {
495 	bus_addr_t addr = bsh + offset;
496 
497 	for (; count != 0; count--, addr += 4)
498 		wr32(addr, value);
499 }
500 
501 /*
502  * Copy `count' 1, 2, 4, or 8 byte values from bus space starting
503  * at tag/bsh1/off1 to bus space starting at tag/bsh2/off2.
504  */
505 void
octopci_bs_c_1(void * t,bus_space_handle_t bsh1,bus_size_t off1,bus_space_handle_t bsh2,bus_size_t off2,size_t count)506 octopci_bs_c_1(void *t, bus_space_handle_t bsh1,
507     bus_size_t off1, bus_space_handle_t bsh2,
508     bus_size_t off2, size_t count)
509 {
510 	bus_addr_t addr1 = bsh1 + off1;
511 	bus_addr_t addr2 = bsh2 + off2;
512 
513 	if (addr1 >= addr2) {
514 		/* src after dest: copy forward */
515 		for (; count != 0; count--, addr1++, addr2++)
516 			wr8(addr2, rd8(addr1));
517 	} else {
518 		/* dest after src: copy backwards */
519 		for (addr1 += (count - 1), addr2 += (count - 1);
520 		    count != 0; count--, addr1--, addr2--)
521 			wr8(addr2, rd8(addr1));
522 	}
523 }
524 
525 void
octopci_bs_c_2(void * t,bus_space_handle_t bsh1,bus_size_t off1,bus_space_handle_t bsh2,bus_size_t off2,size_t count)526 octopci_bs_c_2(void *t, bus_space_handle_t bsh1,
527     bus_size_t off1, bus_space_handle_t bsh2,
528     bus_size_t off2, size_t count)
529 {
530 	bus_addr_t addr1 = bsh1 + off1;
531 	bus_addr_t addr2 = bsh2 + off2;
532 
533 	if (addr1 >= addr2) {
534 		/* src after dest: copy forward */
535 		for (; count != 0; count--, addr1 += 2, addr2 += 2)
536 			wr16(addr2, rd16(addr1));
537 	} else {
538 		/* dest after src: copy backwards */
539 		for (addr1 += 2 * (count - 1), addr2 += 2 * (count - 1);
540 		    count != 0; count--, addr1 -= 2, addr2 -= 2)
541 			wr16(addr2, rd16(addr1));
542 	}
543 }
544 
545 void
octopci_bs_c_4(void * t,bus_space_handle_t bsh1,bus_size_t off1,bus_space_handle_t bsh2,bus_size_t off2,size_t count)546 octopci_bs_c_4(void *t, bus_space_handle_t bsh1,
547     bus_size_t off1, bus_space_handle_t bsh2,
548     bus_size_t off2, size_t count)
549 {
550 	bus_addr_t addr1 = bsh1 + off1;
551 	bus_addr_t addr2 = bsh2 + off2;
552 
553 	if (addr1 >= addr2) {
554 		/* src after dest: copy forward */
555 		for (; count != 0; count--, addr1 += 4, addr2 += 4)
556 			wr32(addr2, rd32(addr1));
557 	} else {
558 		/* dest after src: copy backwards */
559 		for (addr1 += 4 * (count - 1), addr2 += 4 * (count - 1);
560 		    count != 0; count--, addr1 -= 4, addr2 -= 4)
561 			wr32(addr2, rd32(addr1));
562 	}
563 }
564 
565 void
octopci_bs_barrier(void * t __unused,bus_space_handle_t bsh __unused,bus_size_t offset __unused,bus_size_t len __unused,int flags)566 octopci_bs_barrier(void *t __unused,
567 		bus_space_handle_t bsh __unused,
568 		bus_size_t offset __unused, bus_size_t len __unused,
569 		int flags)
570 {
571 #if 0
572 	if (flags & BUS_SPACE_BARRIER_WRITE)
573 		mips_dcache_wbinv_all();
574 #endif
575 }
576