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