Home | History | Annotate | Line # | Download | only in windermere
      1  1.3   thorpej /*	$NetBSD: windermere.c,v 1.3 2021/08/07 16:18:48 thorpej Exp $	*/
      2  1.1  kiyohara /*
      3  1.1  kiyohara  * Copyright (c) 2012 KIYOHARA Takashi
      4  1.1  kiyohara  * All rights reserved.
      5  1.1  kiyohara  *
      6  1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7  1.1  kiyohara  * modification, are permitted provided that the following conditions
      8  1.1  kiyohara  * are met:
      9  1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11  1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14  1.1  kiyohara  *
     15  1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  kiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  kiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  1.1  kiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  1.1  kiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  kiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  1.1  kiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  1.1  kiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  kiyohara  * POSSIBILITY OF SUCH DAMAGE.
     26  1.1  kiyohara  */
     27  1.1  kiyohara 
     28  1.1  kiyohara #include <sys/cdefs.h>
     29  1.3   thorpej __KERNEL_RCSID(0, "$NetBSD: windermere.c,v 1.3 2021/08/07 16:18:48 thorpej Exp $");
     30  1.1  kiyohara 
     31  1.1  kiyohara #include "opt_com.h"
     32  1.1  kiyohara 
     33  1.1  kiyohara #define _INTR_PRIVATE
     34  1.1  kiyohara 
     35  1.1  kiyohara #include <sys/param.h>
     36  1.1  kiyohara #include <sys/bus.h>
     37  1.1  kiyohara #include <sys/device.h>
     38  1.1  kiyohara #include <sys/errno.h>
     39  1.1  kiyohara #include <sys/kernel.h>
     40  1.1  kiyohara #include <sys/systm.h>
     41  1.1  kiyohara #include <sys/timetc.h>
     42  1.1  kiyohara #include <sys/types.h>
     43  1.1  kiyohara 
     44  1.1  kiyohara #include <arm/mainbus/mainbus.h>
     45  1.1  kiyohara #include <arm/pic/picvar.h>
     46  1.1  kiyohara 
     47  1.1  kiyohara #include <machine/epoc32.h>
     48  1.1  kiyohara #include <machine/limits.h>
     49  1.1  kiyohara 
     50  1.1  kiyohara #include <epoc32/windermere/windermerereg.h>
     51  1.1  kiyohara #include <epoc32/windermere/windermerevar.h>
     52  1.1  kiyohara 
     53  1.1  kiyohara #include "locators.h"
     54  1.1  kiyohara 
     55  1.1  kiyohara 
     56  1.1  kiyohara static int windermere_match(device_t, cfdata_t, void *);
     57  1.1  kiyohara static void windermere_attach(device_t parent, device_t self, void *aux);
     58  1.1  kiyohara 
     59  1.1  kiyohara static int windermere_print(void *, const char *);
     60  1.1  kiyohara static int windermere_submatch(device_t, cfdata_t, const int *, void *);
     61  1.1  kiyohara 
     62  1.1  kiyohara static int windermere_find_pending_irqs(void);
     63  1.1  kiyohara 
     64  1.1  kiyohara static void windermere_pic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
     65  1.1  kiyohara static void windermere_pic_block_irqs(struct pic_softc *, size_t, uint32_t);
     66  1.1  kiyohara static void windermere_pic_establish_irq(struct pic_softc *,
     67  1.1  kiyohara 					 struct intrsource *);
     68  1.1  kiyohara 
     69  1.1  kiyohara #define TC_READ(base, off)		(*((base) + (off)))
     70  1.1  kiyohara #define TC_WRITE(base, off, val)	(*((base) + (off)) = (val))
     71  1.1  kiyohara static void windermere_initclocks(void);
     72  1.1  kiyohara static int windermere_clockintr(void *);
     73  1.1  kiyohara static void windermere_tc_init(void);
     74  1.1  kiyohara static u_int windermere_get_timecount(struct timecounter *);
     75  1.1  kiyohara 
     76  1.1  kiyohara static void windermere_delay(unsigned int);
     77  1.1  kiyohara 
     78  1.1  kiyohara CFATTACH_DECL_NEW(windermere, 0,
     79  1.1  kiyohara     windermere_match, windermere_attach, NULL, NULL);
     80  1.1  kiyohara 
     81  1.1  kiyohara static const struct {
     82  1.1  kiyohara 	const char *name;
     83  1.1  kiyohara 	bus_addr_t offset;
     84  1.1  kiyohara 	int irq;
     85  1.1  kiyohara } windermere_periphs[] = {
     86  1.1  kiyohara 	{ "wmlcd",	0x200, WINDERMERECF_IRQ_DEFAULT },
     87  1.1  kiyohara 	{ "wmcom",	0x600, 12 },
     88  1.1  kiyohara 	{ "wmcom",	0x700, 13 },
     89  1.1  kiyohara 	{ "wmrtc",	0xd00, 10 },
     90  1.1  kiyohara 	{ "wmpm",	WINDERMERECF_OFFSET_DEFAULT, WINDERMERECF_IRQ_DEFAULT },
     91  1.1  kiyohara 	{ "wmaudio",	WINDERMERECF_OFFSET_DEFAULT, WINDERMERECF_IRQ_DEFAULT },
     92  1.1  kiyohara 	{ "wmkbd",	0xe28, WINDERMERECF_IRQ_DEFAULT },
     93  1.1  kiyohara };
     94  1.1  kiyohara 
     95  1.1  kiyohara static struct pic_ops windermere_picops = {
     96  1.1  kiyohara 	.pic_unblock_irqs = windermere_pic_unblock_irqs,
     97  1.1  kiyohara 	.pic_block_irqs = windermere_pic_block_irqs,
     98  1.1  kiyohara 	.pic_establish_irq = windermere_pic_establish_irq,
     99  1.1  kiyohara };
    100  1.1  kiyohara static struct pic_softc windermere_pic = {
    101  1.1  kiyohara 	.pic_ops = &windermere_picops,
    102  1.1  kiyohara 	.pic_maxsources = 16,
    103  1.1  kiyohara 	.pic_name = "windermere",
    104  1.1  kiyohara };
    105  1.1  kiyohara static uint16_t pic_mask = 0x0000;
    106  1.1  kiyohara 
    107  1.1  kiyohara /* ARGSUSED */
    108  1.1  kiyohara static int
    109  1.1  kiyohara windermere_match(device_t parent, cfdata_t match, void *aux)
    110  1.1  kiyohara {
    111  1.1  kiyohara 
    112  1.1  kiyohara 	return 1;
    113  1.1  kiyohara }
    114  1.1  kiyohara 
    115  1.1  kiyohara /* ARGSUSED */
    116  1.1  kiyohara static void
    117  1.1  kiyohara windermere_attach(device_t parent, device_t self, void *aux)
    118  1.1  kiyohara {
    119  1.1  kiyohara 	struct mainbus_attach_args *maa = aux;
    120  1.1  kiyohara 	struct windermere_attach_args aa;
    121  1.1  kiyohara 	bus_space_handle_t ioh;
    122  1.1  kiyohara 	int i;
    123  1.1  kiyohara 
    124  1.1  kiyohara 	aprint_naive("\n");
    125  1.1  kiyohara 	aprint_normal("\n");
    126  1.1  kiyohara 
    127  1.1  kiyohara 	if (bus_space_map(maa->mb_iot, maa->mb_iobase, ARM7XX_INTRREG_SIZE, 0,
    128  1.1  kiyohara 								&ioh) != 0) {
    129  1.1  kiyohara 		aprint_error_dev(self, "can't map registers\n");
    130  1.1  kiyohara 		return;
    131  1.1  kiyohara 	}
    132  1.1  kiyohara 
    133  1.1  kiyohara 	pic_add(&windermere_pic, 0);
    134  1.1  kiyohara 	soc_find_pending_irqs = windermere_find_pending_irqs;
    135  1.1  kiyohara 	soc_initclocks = windermere_initclocks;
    136  1.1  kiyohara 	soc_delay = windermere_delay;
    137  1.1  kiyohara 
    138  1.1  kiyohara 	for (i = 0; i < __arraycount(windermere_periphs); i++) {
    139  1.1  kiyohara 		aa.aa_name = windermere_periphs[i].name;
    140  1.1  kiyohara 		aa.aa_iot = maa->mb_iot;
    141  1.1  kiyohara 		aa.aa_ioh = &ioh;
    142  1.1  kiyohara 		aa.aa_offset = windermere_periphs[i].offset;
    143  1.1  kiyohara 		aa.aa_size = 0;
    144  1.1  kiyohara 		aa.aa_irq = windermere_periphs[i].irq;
    145  1.1  kiyohara 
    146  1.2   thorpej 		config_found(self, &aa, windermere_print,
    147  1.3   thorpej 		    CFARGS(.submatch = windermere_submatch));
    148  1.1  kiyohara 	}
    149  1.1  kiyohara }
    150  1.1  kiyohara 
    151  1.1  kiyohara static int
    152  1.1  kiyohara windermere_print(void *aux, const char *pnp)
    153  1.1  kiyohara {
    154  1.1  kiyohara 	struct windermere_attach_args *aa = aux;
    155  1.1  kiyohara 
    156  1.1  kiyohara 	if (pnp)
    157  1.1  kiyohara 		aprint_normal("%s at %s", aa->aa_name, pnp);
    158  1.1  kiyohara 	else {
    159  1.1  kiyohara 		if (aa->aa_offset != WINDERMERECF_OFFSET_DEFAULT) {
    160  1.1  kiyohara 			aprint_normal(" offset 0x%04lx", aa->aa_offset);
    161  1.1  kiyohara 			if (aa->aa_size > 0)
    162  1.1  kiyohara 				aprint_normal("-0x%04lx",
    163  1.1  kiyohara 				    aa->aa_offset + aa->aa_size - 1);
    164  1.1  kiyohara 		}
    165  1.1  kiyohara 		if (aa->aa_irq != WINDERMERECF_IRQ_DEFAULT)
    166  1.1  kiyohara 			aprint_normal(" irq %d", aa->aa_irq);
    167  1.1  kiyohara 	}
    168  1.1  kiyohara 
    169  1.1  kiyohara 	return UNCONF;
    170  1.1  kiyohara }
    171  1.1  kiyohara 
    172  1.1  kiyohara /* ARGSUSED */
    173  1.1  kiyohara static int
    174  1.1  kiyohara windermere_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    175  1.1  kiyohara {
    176  1.1  kiyohara 	struct windermere_attach_args *aa = aux;
    177  1.1  kiyohara 
    178  1.1  kiyohara 	/*
    179  1.1  kiyohara 	 * special case. match *kbd@windermere.
    180  1.1  kiyohara 	 * Windermere helps implement to machine-dependent keyboard.
    181  1.1  kiyohara 	 */
    182  1.1  kiyohara 	if (strcmp(aa->aa_name, "wmkbd") == 0 &&
    183  1.1  kiyohara 	    strcmp(cf->cf_name + strlen(cf->cf_name) - 3, "kbd") == 0)
    184  1.1  kiyohara 		return config_match(parent, cf, aux);
    185  1.1  kiyohara 
    186  1.1  kiyohara 	if (strcmp(cf->cf_name, aa->aa_name) != 0)
    187  1.1  kiyohara 		return 0;
    188  1.1  kiyohara 	return config_match(parent, cf, aux);
    189  1.1  kiyohara }
    190  1.1  kiyohara 
    191  1.1  kiyohara 
    192  1.1  kiyohara static int
    193  1.1  kiyohara windermere_find_pending_irqs(void)
    194  1.1  kiyohara {
    195  1.1  kiyohara 	volatile uint32_t *intr =
    196  1.1  kiyohara 	    (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_INTR_OFFSET);
    197  1.1  kiyohara 	uint16_t pending;
    198  1.1  kiyohara 
    199  1.1  kiyohara 	pending = *(intr + INTSR);
    200  1.1  kiyohara 	if (pending & ~pic_mask) {
    201  1.1  kiyohara 		*(intr + INTENC) = pending & ~pic_mask;
    202  1.1  kiyohara 		printf("stray interrupt pending: 0x%04x\n",
    203  1.1  kiyohara 		    pending & ~pic_mask);
    204  1.1  kiyohara 		pending &= pic_mask;
    205  1.1  kiyohara 	}
    206  1.1  kiyohara 	if (pending == 0)
    207  1.1  kiyohara 		return 0;
    208  1.1  kiyohara 
    209  1.1  kiyohara 	return pic_mark_pending_sources(&windermere_pic, 0, pending);
    210  1.1  kiyohara }
    211  1.1  kiyohara 
    212  1.1  kiyohara /* ARGSUSED */
    213  1.1  kiyohara static void
    214  1.1  kiyohara windermere_pic_unblock_irqs(struct pic_softc *pic, size_t irqbase,
    215  1.1  kiyohara 			    uint32_t irq_mask)
    216  1.1  kiyohara {
    217  1.1  kiyohara 	volatile uint32_t *intr =
    218  1.1  kiyohara 	    (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_INTR_OFFSET);
    219  1.1  kiyohara 
    220  1.1  kiyohara 	*(intr + INTENS) = irq_mask;
    221  1.1  kiyohara 	pic_mask |= irq_mask;
    222  1.1  kiyohara }
    223  1.1  kiyohara 
    224  1.1  kiyohara /* ARGSUSED */
    225  1.1  kiyohara static void
    226  1.1  kiyohara windermere_pic_block_irqs(struct pic_softc *pic, size_t irqbase,
    227  1.1  kiyohara 			  uint32_t irq_mask)
    228  1.1  kiyohara {
    229  1.1  kiyohara 	volatile uint32_t *intr =
    230  1.1  kiyohara 	    (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_INTR_OFFSET);
    231  1.1  kiyohara 
    232  1.1  kiyohara 	*(intr + INTENC) = irq_mask;
    233  1.1  kiyohara 	pic_mask &= ~irq_mask;
    234  1.1  kiyohara }
    235  1.1  kiyohara 
    236  1.1  kiyohara static void
    237  1.1  kiyohara windermere_pic_establish_irq(struct pic_softc *pic, struct intrsource *is)
    238  1.1  kiyohara {
    239  1.1  kiyohara 	/* Nothing */
    240  1.1  kiyohara }
    241  1.1  kiyohara 
    242  1.1  kiyohara static void
    243  1.1  kiyohara windermere_initclocks(void)
    244  1.1  kiyohara {
    245  1.1  kiyohara 	volatile uint32_t *tc1, *tc2;
    246  1.1  kiyohara 	uint32_t ctrl;
    247  1.1  kiyohara 
    248  1.1  kiyohara 	tc1 =
    249  1.1  kiyohara 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC1_OFFSET);
    250  1.1  kiyohara 	tc2 =
    251  1.1  kiyohara 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC2_OFFSET);
    252  1.1  kiyohara 
    253  1.1  kiyohara 	/* set clock to TC1 */
    254  1.1  kiyohara 	ctrl = TC_READ(tc1, TC_CTRL);
    255  1.1  kiyohara 	ctrl |= (CTRL_CLKSEL | CTRL_MODE | CTRL_ENABLE); /* select 512kHz */
    256  1.1  kiyohara 	TC_WRITE(tc1, TC_CTRL, ctrl);
    257  1.1  kiyohara 	TC_WRITE(tc1, TC_LOAD, 512 * 1000 / hz - 1);	/* 512kHz / hz - 1 */
    258  1.1  kiyohara 	intr_establish(8, IPL_CLOCK, 0, windermere_clockintr, NULL);
    259  1.1  kiyohara 
    260  1.1  kiyohara 	/* set delay counter */
    261  1.1  kiyohara 	ctrl = TC_READ(tc2, TC_CTRL);
    262  1.1  kiyohara 	ctrl |= (CTRL_CLKSEL | CTRL_MODE | CTRL_ENABLE); /* select 512kHz */
    263  1.1  kiyohara 	TC_WRITE(tc2, TC_CTRL, ctrl);
    264  1.1  kiyohara 	TC_WRITE(tc2, TC_LOAD, TC_MAX);
    265  1.1  kiyohara 
    266  1.1  kiyohara 	windermere_tc_init();
    267  1.1  kiyohara }
    268  1.1  kiyohara 
    269  1.1  kiyohara static int
    270  1.1  kiyohara windermere_clockintr(void *arg)
    271  1.1  kiyohara {
    272  1.1  kiyohara 	volatile uint32_t *tc1;
    273  1.1  kiyohara 
    274  1.1  kiyohara 	tc1 =
    275  1.1  kiyohara 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC1_OFFSET);
    276  1.1  kiyohara 	TC_WRITE(tc1, TC_EOI, EOI_EOI);
    277  1.1  kiyohara 
    278  1.1  kiyohara 	hardclock(arg);
    279  1.1  kiyohara 
    280  1.1  kiyohara 	return 1;
    281  1.1  kiyohara }
    282  1.1  kiyohara 
    283  1.1  kiyohara static void
    284  1.1  kiyohara windermere_tc_init(void)
    285  1.1  kiyohara {
    286  1.1  kiyohara 	static struct timecounter windermere_tc = {
    287  1.1  kiyohara 		.tc_get_timecount = windermere_get_timecount,
    288  1.1  kiyohara 		.tc_counter_mask = TC_MASK,
    289  1.1  kiyohara 		.tc_frequency = 512000,
    290  1.1  kiyohara 		.tc_name = "windermere",
    291  1.1  kiyohara 		.tc_quality = 100,
    292  1.1  kiyohara 	};
    293  1.1  kiyohara 
    294  1.1  kiyohara 	tc_init(&windermere_tc);
    295  1.1  kiyohara }
    296  1.1  kiyohara 
    297  1.1  kiyohara static u_int
    298  1.1  kiyohara windermere_get_timecount(struct timecounter *tc)
    299  1.1  kiyohara {
    300  1.1  kiyohara 	volatile uint32_t *tc2;
    301  1.1  kiyohara 
    302  1.1  kiyohara 	tc2 =
    303  1.1  kiyohara 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC2_OFFSET);
    304  1.1  kiyohara 
    305  1.1  kiyohara 	return TC_READ(tc2, TC_VAL) ^ TC_MASK;	/* It is decremental counter */
    306  1.1  kiyohara }
    307  1.1  kiyohara 
    308  1.1  kiyohara static void
    309  1.1  kiyohara windermere_delay(unsigned int us)
    310  1.1  kiyohara {
    311  1.1  kiyohara 	volatile uint32_t *tc2;
    312  1.1  kiyohara 	int prev, now, remaining;
    313  1.1  kiyohara 
    314  1.1  kiyohara 	tc2 =
    315  1.1  kiyohara 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC2_OFFSET);
    316  1.1  kiyohara 
    317  1.1  kiyohara 	prev = TC_READ(tc2, TC_VAL) & TC_MASK;
    318  1.1  kiyohara 	remaining = us * 512 / 1000 + 1;
    319  1.1  kiyohara 
    320  1.1  kiyohara 	while (remaining > 0) {
    321  1.1  kiyohara 		now = TC_READ(tc2, TC_VAL) & TC_MASK;
    322  1.1  kiyohara 		if (now >= prev)
    323  1.1  kiyohara 			remaining -= (now - prev);
    324  1.1  kiyohara 		else
    325  1.1  kiyohara 			remaining -= (USHRT_MAX - now + prev + 1);
    326  1.1  kiyohara 		prev = now;
    327  1.1  kiyohara 	}
    328  1.1  kiyohara }
    329