Home | History | Annotate | Line # | Download | only in ee
sif.c revision 1.2
      1 /*	$NetBSD: sif.c,v 1.2 2003/07/15 02:54:37 lukem 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 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: sif.c,v 1.2 2003/07/15 02:54:37 lukem Exp $");
     41 
     42 #include "debug_playstation2.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 
     48 #include <playstation2/playstation2/sifbios.h>
     49 #include <playstation2/ee/dmacvar.h>
     50 #include <playstation2/ee/sifvar.h>
     51 
     52 #ifdef SIF_DEBUG
     53 int __spd_total_alloc;
     54 int	sif_debug = 1;
     55 #define	DPRINTF(fmt, args...)						\
     56 	if (sif_debug)							\
     57 		printf("%s: " fmt, __FUNCTION__ , ##args)
     58 #define	DPRINTFN(n, arg)						\
     59 	if (sif_debug > (n))						\
     60 n		printf("%s: " fmt, __FUNCTION__ , ##args)
     61 #else
     62 #define	DPRINTF(arg...)		((void)0)
     63 #define DPRINTFN(n, arg...)	((void)0)
     64 #endif
     65 
     66 #define ROUND64(x)	((((u_int32_t)(x)) + 63) & ~64)
     67 #define BCD_TO_DECIMAL(x)						\
     68 ({									\
     69 	typeof(x) x_ = x;						\
     70 	(((x_) >> 4) * 10 + ((x_) & 0xf));				\
     71 })
     72 #define MAJOR(x)	BCD_TO_DECIMAL(((x) >> 8) & 0xff)
     73 #define MINOR(x)	BCD_TO_DECIMAL((x) & 0xff)
     74 
     75 void
     76 sif_init()
     77 {
     78 	u_int32_t version;
     79 
     80 	version = sifbios_getver();
     81 	printf("PlayStation 2 SIF BIOS version %d.%d\n",
     82 	    MAJOR(version), MINOR(version));
     83 
     84 	/* Initialize SIF */
     85 	if (sifdma_init() < 0)
     86 		panic("SIFDMA");
     87 	if (sifcmd_init() < 0)
     88 		panic("SIFCMD");
     89 	dmac_intr_establish(D_CH5_SIF0, IPL_TTY, sifcmd_intr, 0);
     90 	if (sifrpc_init() < 0)
     91 		panic("SIFRPC");
     92 	if (iopmem_init() < 0)
     93 		panic("IOP Memory");
     94 }
     95 
     96 void
     97 sif_exit()
     98 {
     99 
    100 	sifrpc_exit();
    101 	sifcmd_exit();
    102 	dmac_intr_disestablish((void *)D_CH5_SIF0);
    103 	sifdma_exit();
    104 }
    105 
    106 int
    107 iopdma_allocate_buffer(struct iopdma_segment *seg, size_t size)
    108 {
    109 	/*
    110 	 * To avoid cache inconsistecy as the result of DMA(to memory),
    111 	 * DMA buffer size is setted to multiple of CPU cache line size (64B)
    112 	 * and aligned to cache line.
    113 	 */
    114 	seg->size = ROUND64(size);
    115 
    116 	seg->iop_paddr = iopmem_alloc(seg->size);
    117 
    118 	if (seg->iop_paddr == NULL) {
    119 		printf("%s: can't allocate IOP memory.\n", __FUNCTION__);
    120 		DPRINTF("request = %d byte, current total = %#x\n",
    121 		    size, __spd_total_alloc);
    122 		return (1);
    123 	}
    124 
    125 	seg->ee_vaddr = IOPPHYS_TO_EEKV(seg->iop_paddr);
    126 
    127 #ifdef SIF_DEBUG
    128 	__spd_total_alloc += size;
    129 #endif
    130 	DPRINTF("0x%08lx+%#x (total=%#x)\n", seg->iop_paddr, size,
    131 	    __spd_total_alloc);
    132 
    133 	KDASSERT((seg->ee_vaddr & 63) == 0);
    134 
    135 	return (0);
    136 }
    137 
    138 void
    139 iopdma_free_buffer(struct iopdma_segment *seg)
    140 {
    141 	int ret;
    142 
    143 	ret = iopmem_free(seg->iop_paddr);
    144 #ifdef SIF_DEBUG
    145 	__spd_total_alloc -= seg->size;
    146 #endif
    147 	DPRINTF("0x%08lx (total=%#x, result=%d)\n", seg->iop_paddr,
    148 	    __spd_total_alloc, ret);
    149 }
    150