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