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