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