Home | History | Annotate | Line # | Download | only in ee
dmac.c revision 1.1.2.1
      1  1.1.2.1  uch /*	$NetBSD: dmac.c,v 1.1.2.1 2001/11/10 16:26:17 uch Exp $	*/
      2      1.1  uch 
      3      1.1  uch /*-
      4      1.1  uch  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5      1.1  uch  * All rights reserved.
      6      1.1  uch  *
      7      1.1  uch  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  uch  * by UCHIYAMA Yasushi.
      9      1.1  uch  *
     10      1.1  uch  * Redistribution and use in source and binary forms, with or without
     11      1.1  uch  * modification, are permitted provided that the following conditions
     12      1.1  uch  * are met:
     13      1.1  uch  * 1. Redistributions of source code must retain the above copyright
     14      1.1  uch  *    notice, this list of conditions and the following disclaimer.
     15      1.1  uch  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  uch  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  uch  *    documentation and/or other materials provided with the distribution.
     18      1.1  uch  * 3. All advertising materials mentioning features or use of this software
     19      1.1  uch  *    must display the following acknowledgement:
     20      1.1  uch  *        This product includes software developed by the NetBSD
     21      1.1  uch  *        Foundation, Inc. and its contributors.
     22      1.1  uch  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1  uch  *    contributors may be used to endorse or promote products derived
     24      1.1  uch  *    from this software without specific prior written permission.
     25      1.1  uch  *
     26      1.1  uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1  uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1  uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1  uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1  uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1  uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1  uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1  uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1  uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1  uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1  uch  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1  uch  */
     38      1.1  uch 
     39      1.1  uch #include "debug_playstation2.h"
     40      1.1  uch 
     41      1.1  uch #include <sys/param.h>
     42      1.1  uch #include <sys/systm.h>
     43      1.1  uch 
     44  1.1.2.1  uch #include <mips/cache.h>
     45  1.1.2.1  uch 
     46      1.1  uch #include <playstation2/ee/eevar.h>
     47      1.1  uch #include <playstation2/ee/dmacvar.h>
     48      1.1  uch #include <playstation2/ee/dmacreg.h>
     49      1.1  uch #include <playstation2/ee/gsvar.h>	/* debug monitor */
     50      1.1  uch 
     51      1.1  uch #include <playstation2/playstation2/interrupt.h>
     52      1.1  uch 
     53      1.1  uch #ifdef DEBUG
     54      1.1  uch #define LEGAL_CHANNEL(x)	((x) >= 0 && (x) <= 15)
     55      1.1  uch #define STATIC
     56      1.1  uch #else
     57      1.1  uch #define STATIC	static
     58      1.1  uch #endif
     59      1.1  uch 
     60      1.1  uch #define _DMAC_NINTR	10
     61      1.1  uch 
     62      1.1  uch STATIC vaddr_t __dmac_channel_base[_DMAC_NINTR] = {
     63      1.1  uch 	D0_REGBASE,
     64      1.1  uch 	D1_REGBASE,
     65      1.1  uch 	D2_REGBASE,
     66      1.1  uch 	D3_REGBASE,
     67      1.1  uch 	D4_REGBASE,
     68      1.1  uch 	D5_REGBASE,
     69      1.1  uch 	D6_REGBASE,
     70      1.1  uch 	D7_REGBASE,
     71      1.1  uch 	D8_REGBASE,
     72      1.1  uch 	D9_REGBASE
     73      1.1  uch };
     74      1.1  uch 
     75      1.1  uch u_int32_t __dmac_enabled_channel;
     76      1.1  uch 
     77      1.1  uch STATIC int __dmac_intialized;
     78      1.1  uch STATIC struct _ipl_dispatcher __dmac_dispatcher[_DMAC_NINTR];
     79      1.1  uch STATIC struct _ipl_holder __dmac_ipl_holder[_IPL_N];
     80      1.1  uch STATIC SLIST_HEAD(, _ipl_dispatcher) __dmac_dispatcher_head =
     81      1.1  uch  SLIST_HEAD_INITIALIZER(__dmac_dispatcher_head);
     82      1.1  uch 
     83      1.1  uch void
     84      1.1  uch dmac_init()
     85      1.1  uch {
     86      1.1  uch 	int i;
     87      1.1  uch 
     88      1.1  uch 	if (__dmac_intialized++)
     89      1.1  uch 		return;
     90      1.1  uch 
     91      1.1  uch 	/* disable DMAC */
     92      1.1  uch 	_reg_write_4(D_ENABLEW_REG, D_ENABLE_SUSPEND);
     93      1.1  uch 
     94      1.1  uch 	/* disable all interrupt */
     95      1.1  uch 	for (i = 0; i < _DMAC_NINTR; i++)
     96      1.1  uch 		dmac_intr_disable(i);
     97      1.1  uch 
     98      1.1  uch 	for (i = 0; i < _IPL_N; i++)
     99      1.1  uch  		__dmac_ipl_holder[i].mask = 0xffffffff;
    100      1.1  uch 
    101      1.1  uch 	if (_reg_read_4(D_STAT_REG) & D_STAT_SIM)
    102      1.1  uch 		_reg_write_4(D_STAT_REG, D_STAT_SIM);
    103      1.1  uch 	if (_reg_read_4(D_STAT_REG) & D_STAT_MEIM)
    104      1.1  uch 		_reg_write_4(D_STAT_REG, D_STAT_MEIM);
    105      1.1  uch 
    106      1.1  uch 	/* clear all status */
    107      1.1  uch 	_reg_write_4(D_STAT_REG, _reg_read_4(D_STAT_REG) & D_STAT_CIS_MASK);
    108      1.1  uch 
    109      1.1  uch 	/* enable DMAC */
    110      1.1  uch 	_reg_write_4(D_ENABLEW_REG, 0);
    111      1.1  uch 	_reg_write_4(D_CTRL_REG, D_CTRL_DMAE);
    112      1.1  uch }
    113      1.1  uch 
    114      1.1  uch /*
    115      1.1  uch  * Interrupt
    116      1.1  uch  */
    117      1.1  uch int
    118      1.1  uch dmac_intr(u_int32_t mask)
    119      1.1  uch {
    120      1.1  uch 	struct _ipl_dispatcher *dispatcher;
    121      1.1  uch 	u_int32_t r, dispatch, pending;
    122      1.1  uch 
    123      1.1  uch 	r = _reg_read_4(D_STAT_REG);
    124      1.1  uch 	mask = D_STAT_CIM(mask);
    125      1.1  uch 	dispatch = r & ~mask & __dmac_enabled_channel;
    126      1.1  uch 	pending = r & mask & __dmac_enabled_channel;
    127      1.1  uch #if 0
    128      1.1  uch 	__gsfb_print(2,
    129      1.1  uch 	    "DMAC stat=%08x, mask=%08x, pend=%08x, disp=%08x enable=%08x\n",
    130      1.1  uch 	    r, mask, pending, dispatch, __dmac_enabled_channel);
    131      1.1  uch #endif
    132      1.1  uch 	if (dispatch == 0)
    133      1.1  uch 		return (pending == 0 ? 1 : 0);
    134      1.1  uch 
    135      1.1  uch 	/* clear interrupt */
    136      1.1  uch 	_reg_write_4(D_STAT_REG, dispatch);
    137      1.1  uch 
    138      1.1  uch 	/* dispatch interrupt handler */
    139      1.1  uch 	SLIST_FOREACH(dispatcher, &__dmac_dispatcher_head, link) {
    140      1.1  uch 		if (dispatcher->bit & dispatch) {
    141      1.1  uch 			KDASSERT(dispatcher->func);
    142      1.1  uch 			(*dispatcher->func)(dispatcher->arg);
    143      1.1  uch 			dispatch &= ~dispatcher->bit;
    144      1.1  uch 		}
    145      1.1  uch 	}
    146      1.1  uch 
    147      1.1  uch 	/* disable spurious interrupt source */
    148      1.1  uch 	if (dispatch) {
    149      1.1  uch 		int i, bit;
    150      1.1  uch 		for (i = 0, bit = 1; i < _DMAC_NINTR; i++, bit <<= 1) {
    151      1.1  uch 			if (bit & dispatch) {
    152      1.1  uch 				dmac_intr_disable(i);
    153      1.1  uch 				printf("%s: spurious interrupt %d disabled.\n",
    154      1.1  uch 				    __FUNCTION__, i);
    155      1.1  uch 			}
    156      1.1  uch 		}
    157      1.1  uch 	}
    158      1.1  uch 
    159      1.1  uch 
    160      1.1  uch 	return (pending == 0 ? 1 : 0);
    161      1.1  uch }
    162      1.1  uch 
    163      1.1  uch void
    164      1.1  uch dmac_intr_enable(enum dmac_channel ch)
    165      1.1  uch {
    166      1.1  uch 	u_int32_t mask;
    167      1.1  uch 
    168      1.1  uch 	KDASSERT(LEGAL_CHANNEL(ch));
    169      1.1  uch 
    170      1.1  uch 	mask = D_STAT_CIM_BIT(ch);
    171      1.1  uch 	_reg_write_4(D_STAT_REG, (_reg_read_4(D_STAT_REG) & mask) ^ mask);
    172      1.1  uch }
    173      1.1  uch 
    174      1.1  uch void
    175      1.1  uch dmac_intr_disable(enum dmac_channel ch)
    176      1.1  uch {
    177      1.1  uch 	KDASSERT(LEGAL_CHANNEL(ch));
    178      1.1  uch 
    179      1.1  uch 	_reg_write_4(D_STAT_REG, _reg_read_4(D_STAT_REG) & D_STAT_CIM_BIT(ch));
    180      1.1  uch }
    181      1.1  uch 
    182      1.1  uch void
    183      1.1  uch dmac_update_mask(u_int32_t mask)
    184      1.1  uch {
    185      1.1  uch 	u_int32_t cur_mask;
    186      1.1  uch 
    187      1.1  uch 	mask = D_STAT_CIM(mask);
    188      1.1  uch 	cur_mask = _reg_read_4(D_STAT_REG);
    189      1.1  uch 
    190      1.1  uch 	_reg_write_4(D_STAT_REG, ((cur_mask ^ ~mask) | (cur_mask & mask)) &
    191      1.1  uch 	    D_STAT_CIM(__dmac_enabled_channel));
    192      1.1  uch }
    193      1.1  uch 
    194      1.1  uch void *
    195      1.1  uch dmac_intr_establish(enum dmac_channel ch, int ipl, int (*func)(void *),
    196      1.1  uch     void *arg)
    197      1.1  uch {
    198      1.1  uch 	struct _ipl_dispatcher *dispatcher = &__dmac_dispatcher[ch];
    199      1.1  uch 	struct _ipl_dispatcher *d;
    200      1.1  uch 	int i, s;
    201      1.1  uch 
    202      1.1  uch 	KDASSERT(dispatcher->func == NULL);
    203      1.1  uch 
    204      1.1  uch 	s = _intr_suspend();
    205      1.1  uch 	dispatcher->func = func;
    206      1.1  uch 	dispatcher->arg = arg;
    207      1.1  uch 	dispatcher->ipl = ipl;
    208      1.1  uch 	dispatcher->channel = ch;
    209      1.1  uch 	dispatcher->bit = D_STAT_CIS_BIT(ch);
    210      1.1  uch 
    211      1.1  uch 	for (i = 0; i < _IPL_N; i++) {
    212      1.1  uch 		if (i < ipl)
    213      1.1  uch 			__dmac_ipl_holder[i].mask &= ~D_STAT_CIM_BIT(ch);
    214      1.1  uch 		else
    215      1.1  uch 			__dmac_ipl_holder[i].mask |= D_STAT_CIM_BIT(ch);
    216      1.1  uch 	}
    217      1.1  uch 
    218      1.1  uch 	/* insert queue IPL order */
    219      1.1  uch 	if (SLIST_EMPTY(&__dmac_dispatcher_head)) {
    220      1.1  uch 		SLIST_INSERT_HEAD(&__dmac_dispatcher_head, dispatcher, link);
    221      1.1  uch 	} else {
    222      1.1  uch 		SLIST_FOREACH(d, &__dmac_dispatcher_head, link) {
    223      1.1  uch 			if (SLIST_NEXT(d, link) == 0 ||
    224      1.1  uch 			    SLIST_NEXT(d, link)->ipl < ipl) {
    225      1.1  uch 				SLIST_INSERT_AFTER(d, dispatcher, link);
    226      1.1  uch 				break;
    227      1.1  uch 			}
    228      1.1  uch 		}
    229      1.1  uch 	}
    230      1.1  uch 
    231      1.1  uch 	md_ipl_register(IPL_DMAC, __dmac_ipl_holder);
    232      1.1  uch 
    233      1.1  uch 	dmac_intr_enable(ch);
    234      1.1  uch 	__dmac_enabled_channel |= D_STAT_CIS_BIT(ch);
    235      1.1  uch 
    236      1.1  uch 	_intr_resume(s);
    237      1.1  uch 
    238      1.1  uch 	return ((void *)ch);
    239      1.1  uch }
    240      1.1  uch 
    241      1.1  uch void
    242      1.1  uch dmac_intr_disestablish(void *handle)
    243      1.1  uch {
    244      1.1  uch 	int ch = (int)(handle);
    245      1.1  uch 	struct _ipl_dispatcher *dispatcher = &__dmac_dispatcher[ch];
    246      1.1  uch 	int i, s;
    247      1.1  uch 
    248      1.1  uch 	s = _intr_suspend();
    249      1.1  uch 
    250      1.1  uch 	dmac_intr_disable(ch);
    251      1.1  uch 	dispatcher->func = NULL;
    252      1.1  uch 
    253      1.1  uch 	SLIST_REMOVE(&__dmac_dispatcher_head, dispatcher,
    254      1.1  uch 	    _ipl_dispatcher, link);
    255      1.1  uch 
    256      1.1  uch 	for (i = 0; i < _IPL_N; i++)
    257      1.1  uch 		__dmac_ipl_holder[i].mask |= D_STAT_CIM_BIT(ch);
    258      1.1  uch 
    259      1.1  uch 	md_ipl_register(IPL_DMAC, __dmac_ipl_holder);
    260      1.1  uch 	__dmac_enabled_channel &= ~D_STAT_CIS_BIT(ch);
    261      1.1  uch 
    262      1.1  uch 	_intr_resume(s);
    263      1.1  uch }
    264      1.1  uch 
    265      1.1  uch /*
    266      1.1  uch  * Start/Stop
    267      1.1  uch  */
    268      1.1  uch void
    269      1.1  uch dmac_start_channel(enum dmac_channel ch)
    270      1.1  uch {
    271      1.1  uch 	bus_addr_t chcr = D_CHCR_REG(__dmac_channel_base[ch]);
    272      1.1  uch 	u_int32_t r;
    273      1.1  uch 	int s;
    274      1.1  uch 
    275      1.1  uch 	/* suspend all channels */
    276      1.1  uch 	s = _intr_suspend();
    277      1.1  uch 	r = _reg_read_4(D_ENABLER_REG);
    278      1.1  uch 	_reg_write_4(D_ENABLEW_REG, r | D_ENABLE_SUSPEND);
    279      1.1  uch 
    280      1.1  uch 	/* access CHCR */
    281      1.1  uch 	_reg_write_4(chcr, (_reg_read_4(chcr) | D_CHCR_STR));
    282      1.1  uch 
    283      1.1  uch 	/* start all channels */
    284      1.1  uch 	_reg_write_4(D_ENABLEW_REG, r & ~D_ENABLE_SUSPEND);
    285      1.1  uch 	_intr_resume(s);
    286      1.1  uch }
    287      1.1  uch 
    288      1.1  uch void
    289      1.1  uch dmac_stop_channel(enum dmac_channel ch)
    290      1.1  uch {
    291      1.1  uch 	bus_addr_t chcr = D_CHCR_REG(__dmac_channel_base[ch]);
    292      1.1  uch 	u_int32_t r;
    293      1.1  uch 	int s;
    294      1.1  uch 
    295      1.1  uch 	/* suspend all channels */
    296      1.1  uch 	s = _intr_suspend();
    297      1.1  uch 	r = _reg_read_4(D_ENABLER_REG);
    298      1.1  uch 	_reg_write_4(D_ENABLEW_REG, r | D_ENABLE_SUSPEND);
    299      1.1  uch 
    300      1.1  uch 	/* access CHCR */
    301      1.1  uch 	_reg_write_4(chcr, (_reg_read_4(chcr) & ~D_CHCR_STR));
    302      1.1  uch 
    303      1.1  uch 	/* resume all chanells */
    304      1.1  uch 	_reg_write_4(D_ENABLEW_REG, r);
    305      1.1  uch 	_intr_resume(s);
    306      1.1  uch }
    307      1.1  uch 
    308      1.1  uch void
    309      1.1  uch dmac_sync_buffer()
    310      1.1  uch {
    311      1.1  uch 
    312  1.1.2.1  uch 	mips_dcache_wbinv_all();
    313      1.1  uch 	__asm__ __volatile("sync.l");
    314      1.1  uch }
    315      1.1  uch 
    316      1.1  uch /*
    317      1.1  uch  * Polling
    318      1.1  uch  *   DMAC status connected to CPCOND[0].
    319      1.1  uch  */
    320      1.1  uch void
    321      1.1  uch dmac_cpc_set(enum dmac_channel ch)
    322      1.1  uch {
    323      1.1  uch 	u_int32_t r;
    324      1.1  uch 
    325      1.1  uch 	r = _reg_read_4(D_PCR_REG);
    326      1.1  uch 	KDASSERT((D_PCR_CPC(r) & ~D_PCR_CPC_BIT(ch)) == 0);
    327      1.1  uch 
    328      1.1  uch 	/* clear interrupt status */
    329      1.1  uch 	_reg_write_4(D_STAT_REG, D_STAT_CIS_BIT(ch));
    330      1.1  uch 
    331      1.1  uch 	_reg_write_4(D_PCR_REG, r | D_PCR_CPC_BIT(ch));
    332      1.1  uch }
    333      1.1  uch 
    334      1.1  uch void
    335      1.1  uch dmac_cpc_clear(enum dmac_channel ch)
    336      1.1  uch {
    337      1.1  uch 
    338      1.1  uch 	_reg_write_4(D_PCR_REG,	_reg_read_4(D_PCR_REG) & ~D_PCR_CPC_BIT(ch))
    339      1.1  uch }
    340      1.1  uch 
    341      1.1  uch void
    342      1.1  uch dmac_cpc_poll()
    343      1.1  uch {
    344      1.1  uch 	__asm__ __volatile__(
    345      1.1  uch 		".set noreorder;"
    346      1.1  uch 	"1:	 nop;"
    347      1.1  uch 		"nop;"
    348      1.1  uch 		"nop;"
    349      1.1  uch 		"nop;"
    350      1.1  uch 		"nop;"
    351      1.1  uch 		"bc0f 1b;"
    352      1.1  uch 		" nop;"
    353      1.1  uch 		".set reorder");
    354      1.1  uch }
    355      1.1  uch 
    356      1.1  uch /* not recommended. use dmac_cpc_poll as possible */
    357      1.1  uch void
    358      1.1  uch dmac_bus_poll(enum dmac_channel ch)
    359      1.1  uch {
    360      1.1  uch 	bus_addr_t chcr = D_CHCR_REG(__dmac_channel_base[ch]);
    361      1.1  uch 
    362      1.1  uch 	while (_reg_read_4(chcr) & D_CHCR_STR)
    363      1.1  uch 		;
    364      1.1  uch }
    365      1.1  uch 
    366      1.1  uch /*
    367      1.1  uch  * Misc
    368      1.1  uch  */
    369      1.1  uch void
    370      1.1  uch dmac_chcr_write(enum dmac_channel ch, u_int32_t v)
    371      1.1  uch {
    372      1.1  uch 	u_int32_t r;
    373      1.1  uch 	int s;
    374      1.1  uch 
    375      1.1  uch 	/* suspend all channels */
    376      1.1  uch 	s = _intr_suspend();
    377      1.1  uch 	r = _reg_read_4(D_ENABLER_REG);
    378      1.1  uch 	_reg_write_4(D_ENABLEW_REG, r | D_ENABLE_SUSPEND);
    379      1.1  uch 
    380      1.1  uch 	/* write CHCR reg */
    381      1.1  uch 	_reg_write_4(D_CHCR_REG(__dmac_channel_base[ch]), v);
    382      1.1  uch 
    383      1.1  uch 	/* resume all chanells */
    384      1.1  uch 	_reg_write_4(D_ENABLEW_REG, r);
    385      1.1  uch 	_intr_resume(s);
    386      1.1  uch }
    387      1.1  uch 
    388