Home | History | Annotate | Line # | Download | only in ee
dmac.c revision 1.6.30.1
      1  1.6.30.1       ad /*	$NetBSD: dmac.c,v 1.6.30.1 2007/04/10 13:23:10 ad 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.3    lukem 
     39       1.3    lukem #include <sys/cdefs.h>
     40  1.6.30.1       ad __KERNEL_RCSID(0, "$NetBSD: dmac.c,v 1.6.30.1 2007/04/10 13:23:10 ad Exp $");
     41       1.1      uch 
     42       1.1      uch #include "debug_playstation2.h"
     43       1.1      uch 
     44       1.1      uch #include <sys/param.h>
     45       1.1      uch #include <sys/systm.h>
     46       1.1      uch 
     47       1.2  thorpej #include <mips/cache.h>
     48       1.2  thorpej 
     49       1.1      uch #include <playstation2/ee/eevar.h>
     50       1.1      uch #include <playstation2/ee/dmacvar.h>
     51       1.1      uch #include <playstation2/ee/dmacreg.h>
     52       1.1      uch #include <playstation2/ee/gsvar.h>	/* debug monitor */
     53       1.1      uch 
     54       1.1      uch #include <playstation2/playstation2/interrupt.h>
     55       1.1      uch 
     56       1.1      uch #ifdef DEBUG
     57       1.1      uch #define LEGAL_CHANNEL(x)	((x) >= 0 && (x) <= 15)
     58       1.1      uch #define STATIC
     59       1.1      uch #else
     60       1.1      uch #define STATIC	static
     61       1.1      uch #endif
     62       1.1      uch 
     63       1.1      uch #define _DMAC_NINTR	10
     64       1.1      uch 
     65       1.1      uch STATIC vaddr_t __dmac_channel_base[_DMAC_NINTR] = {
     66       1.1      uch 	D0_REGBASE,
     67       1.1      uch 	D1_REGBASE,
     68       1.1      uch 	D2_REGBASE,
     69       1.1      uch 	D3_REGBASE,
     70       1.1      uch 	D4_REGBASE,
     71       1.1      uch 	D5_REGBASE,
     72       1.1      uch 	D6_REGBASE,
     73       1.1      uch 	D7_REGBASE,
     74       1.1      uch 	D8_REGBASE,
     75       1.1      uch 	D9_REGBASE
     76       1.1      uch };
     77       1.1      uch 
     78       1.1      uch u_int32_t __dmac_enabled_channel;
     79       1.1      uch 
     80  1.6.30.1       ad STATIC int __dmac_initialized;
     81       1.1      uch STATIC struct _ipl_dispatcher __dmac_dispatcher[_DMAC_NINTR];
     82       1.1      uch STATIC struct _ipl_holder __dmac_ipl_holder[_IPL_N];
     83       1.1      uch STATIC SLIST_HEAD(, _ipl_dispatcher) __dmac_dispatcher_head =
     84       1.1      uch  SLIST_HEAD_INITIALIZER(__dmac_dispatcher_head);
     85       1.1      uch 
     86       1.1      uch void
     87       1.1      uch dmac_init()
     88       1.1      uch {
     89       1.1      uch 	int i;
     90       1.1      uch 
     91  1.6.30.1       ad 	if (__dmac_initialized++)
     92       1.1      uch 		return;
     93       1.1      uch 
     94       1.1      uch 	/* disable DMAC */
     95       1.1      uch 	_reg_write_4(D_ENABLEW_REG, D_ENABLE_SUSPEND);
     96       1.1      uch 
     97       1.1      uch 	/* disable all interrupt */
     98       1.1      uch 	for (i = 0; i < _DMAC_NINTR; i++)
     99       1.1      uch 		dmac_intr_disable(i);
    100       1.1      uch 
    101       1.1      uch 	for (i = 0; i < _IPL_N; i++)
    102       1.1      uch  		__dmac_ipl_holder[i].mask = 0xffffffff;
    103       1.1      uch 
    104       1.1      uch 	if (_reg_read_4(D_STAT_REG) & D_STAT_SIM)
    105       1.1      uch 		_reg_write_4(D_STAT_REG, D_STAT_SIM);
    106       1.1      uch 	if (_reg_read_4(D_STAT_REG) & D_STAT_MEIM)
    107       1.1      uch 		_reg_write_4(D_STAT_REG, D_STAT_MEIM);
    108       1.1      uch 
    109       1.1      uch 	/* clear all status */
    110       1.1      uch 	_reg_write_4(D_STAT_REG, _reg_read_4(D_STAT_REG) & D_STAT_CIS_MASK);
    111       1.1      uch 
    112       1.1      uch 	/* enable DMAC */
    113       1.1      uch 	_reg_write_4(D_ENABLEW_REG, 0);
    114       1.1      uch 	_reg_write_4(D_CTRL_REG, D_CTRL_DMAE);
    115       1.1      uch }
    116       1.1      uch 
    117       1.1      uch /*
    118       1.1      uch  * Interrupt
    119       1.1      uch  */
    120       1.1      uch int
    121       1.1      uch dmac_intr(u_int32_t mask)
    122       1.1      uch {
    123       1.1      uch 	struct _ipl_dispatcher *dispatcher;
    124       1.1      uch 	u_int32_t r, dispatch, pending;
    125       1.1      uch 
    126       1.1      uch 	r = _reg_read_4(D_STAT_REG);
    127       1.1      uch 	mask = D_STAT_CIM(mask);
    128       1.1      uch 	dispatch = r & ~mask & __dmac_enabled_channel;
    129       1.1      uch 	pending = r & mask & __dmac_enabled_channel;
    130       1.1      uch #if 0
    131       1.1      uch 	__gsfb_print(2,
    132       1.1      uch 	    "DMAC stat=%08x, mask=%08x, pend=%08x, disp=%08x enable=%08x\n",
    133       1.1      uch 	    r, mask, pending, dispatch, __dmac_enabled_channel);
    134       1.1      uch #endif
    135       1.1      uch 	if (dispatch == 0)
    136       1.1      uch 		return (pending == 0 ? 1 : 0);
    137       1.1      uch 
    138       1.1      uch 	/* clear interrupt */
    139       1.1      uch 	_reg_write_4(D_STAT_REG, dispatch);
    140       1.1      uch 
    141       1.1      uch 	/* dispatch interrupt handler */
    142       1.1      uch 	SLIST_FOREACH(dispatcher, &__dmac_dispatcher_head, link) {
    143       1.1      uch 		if (dispatcher->bit & dispatch) {
    144       1.1      uch 			KDASSERT(dispatcher->func);
    145       1.1      uch 			(*dispatcher->func)(dispatcher->arg);
    146       1.1      uch 			dispatch &= ~dispatcher->bit;
    147       1.1      uch 		}
    148       1.1      uch 	}
    149       1.1      uch 
    150       1.1      uch 	/* disable spurious interrupt source */
    151       1.1      uch 	if (dispatch) {
    152       1.1      uch 		int i, bit;
    153       1.1      uch 		for (i = 0, bit = 1; i < _DMAC_NINTR; i++, bit <<= 1) {
    154       1.1      uch 			if (bit & dispatch) {
    155       1.1      uch 				dmac_intr_disable(i);
    156       1.1      uch 				printf("%s: spurious interrupt %d disabled.\n",
    157       1.1      uch 				    __FUNCTION__, i);
    158       1.1      uch 			}
    159       1.1      uch 		}
    160       1.1      uch 	}
    161       1.1      uch 
    162       1.1      uch 
    163       1.1      uch 	return (pending == 0 ? 1 : 0);
    164       1.1      uch }
    165       1.1      uch 
    166       1.1      uch void
    167       1.1      uch dmac_intr_enable(enum dmac_channel ch)
    168       1.1      uch {
    169       1.1      uch 	u_int32_t mask;
    170       1.1      uch 
    171       1.1      uch 	KDASSERT(LEGAL_CHANNEL(ch));
    172       1.1      uch 
    173       1.1      uch 	mask = D_STAT_CIM_BIT(ch);
    174       1.1      uch 	_reg_write_4(D_STAT_REG, (_reg_read_4(D_STAT_REG) & mask) ^ mask);
    175       1.1      uch }
    176       1.1      uch 
    177       1.1      uch void
    178       1.1      uch dmac_intr_disable(enum dmac_channel ch)
    179       1.1      uch {
    180       1.1      uch 	KDASSERT(LEGAL_CHANNEL(ch));
    181       1.1      uch 
    182       1.1      uch 	_reg_write_4(D_STAT_REG, _reg_read_4(D_STAT_REG) & D_STAT_CIM_BIT(ch));
    183       1.1      uch }
    184       1.1      uch 
    185       1.1      uch void
    186       1.1      uch dmac_update_mask(u_int32_t mask)
    187       1.1      uch {
    188       1.1      uch 	u_int32_t cur_mask;
    189       1.1      uch 
    190       1.1      uch 	mask = D_STAT_CIM(mask);
    191       1.1      uch 	cur_mask = _reg_read_4(D_STAT_REG);
    192       1.1      uch 
    193       1.1      uch 	_reg_write_4(D_STAT_REG, ((cur_mask ^ ~mask) | (cur_mask & mask)) &
    194       1.1      uch 	    D_STAT_CIM(__dmac_enabled_channel));
    195       1.1      uch }
    196       1.1      uch 
    197       1.1      uch void *
    198       1.1      uch dmac_intr_establish(enum dmac_channel ch, int ipl, int (*func)(void *),
    199       1.1      uch     void *arg)
    200       1.1      uch {
    201       1.1      uch 	struct _ipl_dispatcher *dispatcher = &__dmac_dispatcher[ch];
    202       1.1      uch 	struct _ipl_dispatcher *d;
    203       1.1      uch 	int i, s;
    204       1.1      uch 
    205       1.1      uch 	KDASSERT(dispatcher->func == NULL);
    206       1.1      uch 
    207       1.1      uch 	s = _intr_suspend();
    208       1.1      uch 	dispatcher->func = func;
    209       1.1      uch 	dispatcher->arg = arg;
    210       1.1      uch 	dispatcher->ipl = ipl;
    211       1.1      uch 	dispatcher->channel = ch;
    212       1.1      uch 	dispatcher->bit = D_STAT_CIS_BIT(ch);
    213       1.1      uch 
    214       1.1      uch 	for (i = 0; i < _IPL_N; i++) {
    215       1.1      uch 		if (i < ipl)
    216       1.1      uch 			__dmac_ipl_holder[i].mask &= ~D_STAT_CIM_BIT(ch);
    217       1.1      uch 		else
    218       1.1      uch 			__dmac_ipl_holder[i].mask |= D_STAT_CIM_BIT(ch);
    219       1.1      uch 	}
    220       1.1      uch 
    221       1.1      uch 	/* insert queue IPL order */
    222       1.1      uch 	if (SLIST_EMPTY(&__dmac_dispatcher_head)) {
    223       1.1      uch 		SLIST_INSERT_HEAD(&__dmac_dispatcher_head, dispatcher, link);
    224       1.1      uch 	} else {
    225       1.1      uch 		SLIST_FOREACH(d, &__dmac_dispatcher_head, link) {
    226       1.1      uch 			if (SLIST_NEXT(d, link) == 0 ||
    227       1.1      uch 			    SLIST_NEXT(d, link)->ipl < ipl) {
    228       1.1      uch 				SLIST_INSERT_AFTER(d, dispatcher, link);
    229       1.1      uch 				break;
    230       1.1      uch 			}
    231       1.1      uch 		}
    232       1.1      uch 	}
    233       1.1      uch 
    234       1.1      uch 	md_ipl_register(IPL_DMAC, __dmac_ipl_holder);
    235       1.1      uch 
    236       1.1      uch 	dmac_intr_enable(ch);
    237       1.1      uch 	__dmac_enabled_channel |= D_STAT_CIS_BIT(ch);
    238       1.1      uch 
    239       1.1      uch 	_intr_resume(s);
    240       1.1      uch 
    241       1.1      uch 	return ((void *)ch);
    242       1.1      uch }
    243       1.1      uch 
    244       1.1      uch void
    245       1.1      uch dmac_intr_disestablish(void *handle)
    246       1.1      uch {
    247       1.1      uch 	int ch = (int)(handle);
    248       1.1      uch 	struct _ipl_dispatcher *dispatcher = &__dmac_dispatcher[ch];
    249       1.1      uch 	int i, s;
    250       1.1      uch 
    251       1.1      uch 	s = _intr_suspend();
    252       1.1      uch 
    253       1.1      uch 	dmac_intr_disable(ch);
    254       1.1      uch 	dispatcher->func = NULL;
    255       1.1      uch 
    256       1.1      uch 	SLIST_REMOVE(&__dmac_dispatcher_head, dispatcher,
    257       1.1      uch 	    _ipl_dispatcher, link);
    258       1.1      uch 
    259       1.1      uch 	for (i = 0; i < _IPL_N; i++)
    260       1.1      uch 		__dmac_ipl_holder[i].mask |= D_STAT_CIM_BIT(ch);
    261       1.1      uch 
    262       1.1      uch 	md_ipl_register(IPL_DMAC, __dmac_ipl_holder);
    263       1.1      uch 	__dmac_enabled_channel &= ~D_STAT_CIS_BIT(ch);
    264       1.1      uch 
    265       1.1      uch 	_intr_resume(s);
    266       1.1      uch }
    267       1.1      uch 
    268       1.1      uch /*
    269       1.1      uch  * Start/Stop
    270       1.1      uch  */
    271       1.1      uch void
    272       1.1      uch dmac_start_channel(enum dmac_channel ch)
    273       1.1      uch {
    274       1.1      uch 	bus_addr_t chcr = D_CHCR_REG(__dmac_channel_base[ch]);
    275       1.1      uch 	u_int32_t r;
    276       1.1      uch 	int s;
    277       1.1      uch 
    278       1.1      uch 	/* suspend all channels */
    279       1.1      uch 	s = _intr_suspend();
    280       1.1      uch 	r = _reg_read_4(D_ENABLER_REG);
    281       1.1      uch 	_reg_write_4(D_ENABLEW_REG, r | D_ENABLE_SUSPEND);
    282       1.1      uch 
    283       1.1      uch 	/* access CHCR */
    284       1.1      uch 	_reg_write_4(chcr, (_reg_read_4(chcr) | D_CHCR_STR));
    285       1.1      uch 
    286       1.1      uch 	/* start all channels */
    287       1.1      uch 	_reg_write_4(D_ENABLEW_REG, r & ~D_ENABLE_SUSPEND);
    288       1.1      uch 	_intr_resume(s);
    289       1.1      uch }
    290       1.1      uch 
    291       1.1      uch void
    292       1.1      uch dmac_stop_channel(enum dmac_channel ch)
    293       1.1      uch {
    294       1.1      uch 	bus_addr_t chcr = D_CHCR_REG(__dmac_channel_base[ch]);
    295       1.1      uch 	u_int32_t r;
    296       1.1      uch 	int s;
    297       1.1      uch 
    298       1.1      uch 	/* suspend all channels */
    299       1.1      uch 	s = _intr_suspend();
    300       1.1      uch 	r = _reg_read_4(D_ENABLER_REG);
    301       1.1      uch 	_reg_write_4(D_ENABLEW_REG, r | D_ENABLE_SUSPEND);
    302       1.1      uch 
    303       1.1      uch 	/* access CHCR */
    304       1.1      uch 	_reg_write_4(chcr, (_reg_read_4(chcr) & ~D_CHCR_STR));
    305       1.1      uch 
    306       1.1      uch 	/* resume all chanells */
    307       1.1      uch 	_reg_write_4(D_ENABLEW_REG, r);
    308       1.1      uch 	_intr_resume(s);
    309       1.1      uch }
    310       1.1      uch 
    311       1.1      uch void
    312       1.1      uch dmac_sync_buffer()
    313       1.1      uch {
    314       1.1      uch 
    315       1.2  thorpej 	mips_dcache_wbinv_all();
    316       1.6    perry 	__asm volatile("sync.l");
    317       1.1      uch }
    318       1.1      uch 
    319       1.1      uch /*
    320       1.1      uch  * Polling
    321       1.1      uch  *   DMAC status connected to CPCOND[0].
    322       1.1      uch  */
    323       1.1      uch void
    324       1.1      uch dmac_cpc_set(enum dmac_channel ch)
    325       1.1      uch {
    326       1.1      uch 	u_int32_t r;
    327       1.1      uch 
    328       1.1      uch 	r = _reg_read_4(D_PCR_REG);
    329       1.1      uch 	KDASSERT((D_PCR_CPC(r) & ~D_PCR_CPC_BIT(ch)) == 0);
    330       1.1      uch 
    331       1.1      uch 	/* clear interrupt status */
    332       1.1      uch 	_reg_write_4(D_STAT_REG, D_STAT_CIS_BIT(ch));
    333       1.1      uch 
    334       1.1      uch 	_reg_write_4(D_PCR_REG, r | D_PCR_CPC_BIT(ch));
    335       1.1      uch }
    336       1.1      uch 
    337       1.1      uch void
    338       1.1      uch dmac_cpc_clear(enum dmac_channel ch)
    339       1.1      uch {
    340       1.1      uch 
    341       1.1      uch 	_reg_write_4(D_PCR_REG,	_reg_read_4(D_PCR_REG) & ~D_PCR_CPC_BIT(ch))
    342       1.1      uch }
    343       1.1      uch 
    344       1.1      uch void
    345       1.1      uch dmac_cpc_poll()
    346       1.1      uch {
    347       1.6    perry 	__asm volatile(
    348       1.1      uch 		".set noreorder;"
    349       1.1      uch 	"1:	 nop;"
    350       1.1      uch 		"nop;"
    351       1.1      uch 		"nop;"
    352       1.1      uch 		"nop;"
    353       1.1      uch 		"nop;"
    354       1.1      uch 		"bc0f 1b;"
    355       1.1      uch 		" nop;"
    356       1.1      uch 		".set reorder");
    357       1.1      uch }
    358       1.1      uch 
    359       1.1      uch /* not recommended. use dmac_cpc_poll as possible */
    360       1.1      uch void
    361       1.1      uch dmac_bus_poll(enum dmac_channel ch)
    362       1.1      uch {
    363       1.1      uch 	bus_addr_t chcr = D_CHCR_REG(__dmac_channel_base[ch]);
    364       1.1      uch 
    365       1.1      uch 	while (_reg_read_4(chcr) & D_CHCR_STR)
    366       1.1      uch 		;
    367       1.1      uch }
    368       1.1      uch 
    369       1.1      uch /*
    370       1.1      uch  * Misc
    371       1.1      uch  */
    372       1.1      uch void
    373       1.1      uch dmac_chcr_write(enum dmac_channel ch, u_int32_t v)
    374       1.1      uch {
    375       1.1      uch 	u_int32_t r;
    376       1.1      uch 	int s;
    377       1.1      uch 
    378       1.1      uch 	/* suspend all channels */
    379       1.1      uch 	s = _intr_suspend();
    380       1.1      uch 	r = _reg_read_4(D_ENABLER_REG);
    381       1.1      uch 	_reg_write_4(D_ENABLEW_REG, r | D_ENABLE_SUSPEND);
    382       1.1      uch 
    383       1.1      uch 	/* write CHCR reg */
    384       1.1      uch 	_reg_write_4(D_CHCR_REG(__dmac_channel_base[ch]), v);
    385       1.1      uch 
    386       1.1      uch 	/* resume all chanells */
    387       1.1      uch 	_reg_write_4(D_ENABLEW_REG, r);
    388       1.1      uch 	_intr_resume(s);
    389       1.1      uch }
    390       1.1      uch 
    391