xref: /netbsd-src/sys/arch/playstation2/ee/sif.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: sif.c,v 1.10 2014/03/31 11:25:49 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sif.c,v 1.10 2014/03/31 11:25:49 martin Exp $");
34 
35 #include "debug_playstation2.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 
41 #include <playstation2/playstation2/sifbios.h>
42 #include <playstation2/ee/dmacvar.h>
43 #include <playstation2/ee/sifvar.h>
44 
45 #ifdef SIF_DEBUG
46 int __spd_total_alloc;
47 int	sif_debug = 1;
48 #define	DPRINTF(fmt, args...)						\
49 	if (sif_debug)							\
50 		printf("%s: " fmt, __func__ , ##args)
51 #define	DPRINTFN(n, arg)						\
52 	if (sif_debug > (n))						\
53 n		printf("%s: " fmt, __func__ , ##args)
54 #else
55 #define	DPRINTF(arg...)		((void)0)
56 #define DPRINTFN(n, arg...)	((void)0)
57 #endif
58 
59 #define ROUND64(x)	((((u_int32_t)(x)) + 63) & ~64)
60 #define BCD_TO_DECIMAL(x)						\
61 ({									\
62 	typeof(x) x_ = x;						\
63 	(((x_) >> 4) * 10 + ((x_) & 0xf));				\
64 })
65 #define MAJOR(x)	BCD_TO_DECIMAL(((x) >> 8) & 0xff)
66 #define MINOR(x)	BCD_TO_DECIMAL((x) & 0xff)
67 
68 void
69 sif_init(void)
70 {
71 	u_int32_t vers;
72 
73 	vers = sifbios_getver();
74 	printf("PlayStation 2 SIF BIOS version %d.%d\n",
75 	    MAJOR(vers), MINOR(vers));
76 
77 	/* Initialize SIF */
78 	if (sifdma_init() < 0)
79 		panic("SIFDMA");
80 	if (sifcmd_init() < 0)
81 		panic("SIFCMD");
82 	dmac_intr_establish(D_CH5_SIF0, IPL_TTY, sifcmd_intr, 0);
83 	if (sifrpc_init() < 0)
84 		panic("SIFRPC");
85 	if (iopmem_init() < 0)
86 		panic("IOP Memory");
87 }
88 
89 void
90 sif_exit(void)
91 {
92 
93 	sifrpc_exit();
94 	sifcmd_exit();
95 	dmac_intr_disestablish((void *)D_CH5_SIF0);
96 	sifdma_exit();
97 }
98 
99 int
100 iopdma_allocate_buffer(struct iopdma_segment *seg, size_t size)
101 {
102 	/*
103 	 * To avoid cache inconsistecy as the result of DMA(to memory),
104 	 * DMA buffer size is setted to multiple of CPU cache line size (64B)
105 	 * and aligned to cache line.
106 	 */
107 	seg->size = ROUND64(size);
108 
109 	seg->iop_paddr = iopmem_alloc(seg->size);
110 
111 	if (seg->iop_paddr == 0) {
112 		printf("%s: can't allocate IOP memory.\n", __func__);
113 		DPRINTF("request = %d byte, current total = %#x\n",
114 		    size, __spd_total_alloc);
115 		return (1);
116 	}
117 
118 	seg->ee_vaddr = IOPPHYS_TO_EEKV(seg->iop_paddr);
119 
120 #ifdef SIF_DEBUG
121 	__spd_total_alloc += size;
122 #endif
123 	DPRINTF("0x%08lx+%#x (total=%#x)\n", seg->iop_paddr, size,
124 	    __spd_total_alloc);
125 
126 	KDASSERT((seg->ee_vaddr & 63) == 0);
127 
128 	return (0);
129 }
130 
131 void
132 iopdma_free_buffer(struct iopdma_segment *seg)
133 {
134 	int ret;
135 
136 	ret = iopmem_free(seg->iop_paddr);
137 #ifdef SIF_DEBUG
138 	__spd_total_alloc -= seg->size;
139 #endif
140 	DPRINTF("0x%08lx (total=%#x, result=%d)\n", seg->iop_paddr,
141 	    __spd_total_alloc, ret);
142 }
143