1 /*        $NetBSD: hd64465.c,v 1.19 2021/08/07 16:18:55 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: hd64465.c,v 1.19 2021/08/07 16:18:55 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/boot_flag.h>
39 #include <sys/bus.h>
40 
41 #include <machine/intr.h>
42 #include <machine/debug.h>
43 
44 #include <sh3/intcreg.h>
45 #include <hpcsh/dev/hd64465/hd64465var.h>
46 #include <hpcsh/dev/hd64465/hd64465reg.h>
47 #include <hpcsh/dev/hd64465/hd64465intcreg.h>
48 
49 /* HD64465 modules. */
50 STATIC const struct hd64465_module {
51           const char *name;
52 } hd64465_modules[] = {
53           [HD64465_MODULE_PS2IF]                  = { "hd64465ps2if" },
54           [HD64465_MODULE_PCMCIA]                 = { "hd64465pcmcia" },
55           [HD64465_MODULE_AFE]                    = { "hd64465afe" },
56           [HD64465_MODULE_GPIO]                   = { "hd64465gpio" },
57           [HD64465_MODULE_KBC]                    = { "hd64465kbc" },
58           [HD64465_MODULE_IRDA]                   = { "hd64465irda" },
59           [HD64465_MODULE_UART]                   = { "hd64465uart" },
60           [HD64465_MODULE_PARALEL]      = { "hd64465paralel" },
61           [HD64465_MODULE_CODEC]                  = { "hd64465codec" },
62           [HD64465_MODULE_OHCI]                   = { "hd64465ohci" },
63           [HD64465_MODULE_ADC]                    = { "hd64465adc" }
64 };
65 
66 STATIC int hd64465_match(device_t, cfdata_t, void *);
67 STATIC void hd64465_attach(device_t, device_t, void *);
68 STATIC int hd64465_print(void *, const char *);
69 #ifdef DEBUG
70 STATIC void hd64465_info(void);
71 #endif
72 
73 CFATTACH_DECL_NEW(hd64465if, 0,
74     hd64465_match, hd64465_attach, NULL, NULL);
75 
76 int
hd64465_match(device_t parent,cfdata_t cf,void * aux)77 hd64465_match(device_t parent, cfdata_t cf, void *aux)
78 {
79 
80           if (strcmp("hd64465if", cf->cf_name))
81                     return (0);
82 
83           if (hd64465_reg_read_2(HD64465_SDIDR) != 0x8122) {
84                     /* not HD64465 */
85                     return (0);
86           }
87 
88           return (1);
89 }
90 
91 void
hd64465_attach(device_t parent,device_t self,void * aux)92 hd64465_attach(device_t parent, device_t self, void *aux)
93 {
94           const struct hd64465_module *module;
95           struct hd64465_attach_args ha;
96           uint16_t r;
97           size_t i;
98 
99           printf("\n");
100 #ifdef DEBUG
101           if (bootverbose)
102                     hd64465_info();
103 #endif
104 
105           r = hd64465_reg_read_2(HD64465_SRR);
106           printf("%s: HITACHI HD64465 rev. %d.%d\n", device_xname(self),
107               (r >> 8) & 0xff, r & 0xff);
108 
109           /* Mask all interrupt */
110           hd64465_reg_write_2(HD64465_NIMR, 0xffff);
111           /* Edge trigger mode to clear pending interrupt. */
112           hd64465_reg_write_2(HD64465_NITR, 0xffff);
113           /* Clear pending interrupt */
114           hd64465_reg_write_2(HD64465_NIRR, 0x0000);
115 
116           /* Attach all sub modules */
117           for (i = 0, module = hd64465_modules;
118               i < __arraycount(hd64465_modules);
119               i++, module++) {
120                     if (module->name == 0)
121                               continue;
122                     ha.ha_module_id = i;
123                     config_found(self, &ha, hd64465_print, CFARGS_NONE);
124           }
125 }
126 
127 int
hd64465_print(void * aux,const char * pnp)128 hd64465_print(void *aux, const char *pnp)
129 {
130           struct hd64465_attach_args *ha = aux;
131 
132           if (pnp)
133                     aprint_normal("%s at %s",
134                         hd64465_modules[ha->ha_module_id].name, pnp);
135 
136           return (UNCONF);
137 }
138 
139 void *
hd64465_intr_establish(int irq,int mode,int level,int (* func)(void *),void * arg)140 hd64465_intr_establish(int irq, int mode, int level,
141     int (*func)(void *), void *arg)
142 {
143           uint16_t r;
144           int s;
145 
146           s = splhigh();
147           /* Trigger type */
148           r = hd64465_reg_read_2(HD64465_NITR);
149           switch (mode) {
150           case IST_PULSE:
151                     /* FALLTHROUGH */
152           case IST_EDGE:
153                     r |= irq;
154                     break;
155           case IST_LEVEL:
156                     r &= ~irq;
157                     break;
158           }
159           hd64465_reg_write_2(HD64465_NITR, r);
160 
161           hd6446x_intr_establish(irq, mode, level, func, arg);
162           splx(s);
163 
164           return (void *)irq;
165 }
166 
167 void
hd64465_intr_disestablish(void * handle)168 hd64465_intr_disestablish(void *handle)
169 {
170 
171           hd6446x_intr_disestablish(handle);
172 }
173 
174 /* For the sake of Windows CE reboot clearly. */
175 void
hd64465_shutdown(void)176 hd64465_shutdown(void)
177 {
178 
179           /* Enable all interrupt */
180           hd64465_reg_write_2(HD64465_NIMR, 0x0000);
181 
182           /* Level trigger mode */
183           hd64465_reg_write_2(HD64465_NITR, 0x0000);
184 }
185 
186 void
hd64465_info(void)187 hd64465_info(void)
188 {
189           uint16_t r;
190 
191           dbg_bit_print_msg(_reg_read_2(SH4_ICR), "SH4_ICR");
192           r = hd64465_reg_read_2(HD64465_NIMR);
193           dbg_bit_print_msg(r, "NIMR");
194           r = hd64465_reg_read_2(HD64465_NIRR);
195           dbg_bit_print_msg(r, "NIRR");
196           r = hd64465_reg_read_2(HD64465_NITR);
197           dbg_bit_print_msg(r, "NITR");
198 }
199