1 /* $NetBSD: obio.c,v 1.11 2021/08/07 16:18:57 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
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: obio.c,v 1.11 2021/08/07 16:18:57 thorpej Exp $");
34
35 #include "btn_obio.h"
36 #include "pwrsw_obio.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41
42 #include <uvm/uvm_extern.h>
43
44 #include <sh3/devreg.h>
45 #include <sh3/mmu.h>
46 #include <sh3/pmap.h>
47 #include <sh3/pte.h>
48
49 #include <sys/bus.h>
50 #include <machine/cpu.h>
51 #include <machine/intr.h>
52
53 #include <landisk/dev/obiovar.h>
54
55 #if (NPWRSW_OBIO > 0) || (NBTN_OBIO > 0)
56 #include <dev/sysmon/sysmonvar.h>
57 #include <dev/sysmon/sysmon_taskq.h>
58 #endif
59
60 #include "locators.h"
61
62
63 struct obio_softc {
64 device_t sc_dev;
65
66 bus_space_tag_t sc_iot; /* io space tag */
67 bus_space_tag_t sc_memt; /* mem space tag */
68 };
69
70 static int obio_match(device_t, cfdata_t, void *);
71 static void obio_attach(device_t, device_t, void *);
72 static int obio_print(void *, const char *);
73 static int obio_search(device_t, cfdata_t, const int *, void *);
74
75 CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
76 obio_match, obio_attach, NULL, NULL);
77
78 static int
obio_match(device_t parent,cfdata_t cf,void * aux)79 obio_match(device_t parent, cfdata_t cf, void *aux)
80 {
81 struct obiobus_attach_args *oba = aux;
82
83 if (strcmp(oba->oba_busname, cf->cf_name))
84 return (0);
85
86 return (1);
87 }
88
89 static void
obio_attach(device_t parent,device_t self,void * aux)90 obio_attach(device_t parent, device_t self, void *aux)
91 {
92 struct obio_softc *sc = device_private(self);
93 struct obiobus_attach_args *oba = aux;
94
95 aprint_naive("\n");
96 aprint_normal("\n");
97
98 sc->sc_dev = self;
99
100 sc->sc_iot = oba->oba_iot;
101 sc->sc_memt = oba->oba_memt;
102
103 #if (NPWRSW_OBIO > 0) || (NBTN_OBIO > 0)
104 sysmon_power_settype("landisk");
105 sysmon_task_queue_init();
106 #endif
107
108 config_search(self, NULL,
109 CFARGS(.search = obio_search));
110 }
111
112 static int
obio_search(device_t parent,cfdata_t cf,const int * ldesc,void * aux)113 obio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
114 {
115 struct obio_io res_io[1];
116 struct obio_iomem res_mem[1];
117 struct obio_irq res_irq[1];
118 struct obio_softc *sc = device_private(parent);
119 struct obio_attach_args oa;
120 int tryagain;
121
122 do {
123 oa.oa_iot = sc->sc_iot;
124 oa.oa_memt = sc->sc_memt;
125
126 res_io[0].or_addr = cf->cf_iobase;
127 res_io[0].or_size = cf->cf_iosize;
128
129 res_mem[0].or_addr = cf->cf_maddr;
130 res_mem[0].or_size = cf->cf_msize;
131
132 res_irq[0].or_irq = cf->cf_irq;
133
134 oa.oa_io = res_io;
135 oa.oa_nio = 1;
136
137 oa.oa_iomem = res_mem;
138 oa.oa_niomem = 1;
139
140 oa.oa_irq = res_irq;
141 oa.oa_nirq = 1;
142
143 tryagain = 0;
144 if (config_probe(parent, cf, &oa)) {
145 config_attach(parent, cf, &oa, obio_print, CFARGS_NONE);
146 tryagain = (cf->cf_fstate == FSTATE_STAR);
147 }
148 } while (tryagain);
149
150 return (0);
151 }
152
153 static int
obio_print(void * args,const char * name)154 obio_print(void *args, const char *name)
155 {
156 struct obio_attach_args *oa = args;
157 const char *sep;
158 int i;
159
160 if (oa->oa_nio) {
161 sep = "";
162 aprint_normal(" port ");
163 for (i = 0; i < oa->oa_nio; i++) {
164 if (oa->oa_io[i].or_size == 0)
165 continue;
166 aprint_normal("%s0x%x", sep, oa->oa_io[i].or_addr);
167 if (oa->oa_io[i].or_size > 1)
168 aprint_normal("-0x%x", oa->oa_io[i].or_addr +
169 oa->oa_io[i].or_size - 1);
170 sep = ",";
171 }
172 }
173
174 if (oa->oa_niomem) {
175 sep = "";
176 aprint_normal(" iomem ");
177 for (i = 0; i < oa->oa_niomem; i++) {
178 if (oa->oa_iomem[i].or_size == 0)
179 continue;
180 aprint_normal("%s0x%x", sep, oa->oa_iomem[i].or_addr);
181 if (oa->oa_iomem[i].or_size > 1)
182 aprint_normal("-0x%x", oa->oa_iomem[i].or_addr +
183 oa->oa_iomem[i].or_size - 1);
184 sep = ",";
185 }
186 }
187
188 if (oa->oa_nirq) {
189 sep = "";
190 aprint_normal(" irq ");
191 for (i = 0; i < oa->oa_nirq; i++) {
192 if (oa->oa_irq[i].or_irq == IRQUNK)
193 continue;
194 aprint_normal("%s%d", sep, oa->oa_irq[i].or_irq);
195 sep = ",";
196 }
197 }
198
199 return (UNCONF);
200 }
201
202 /*
203 * Set up an interrupt handler to start being called.
204 */
205 void *
obio_intr_establish(int irq,int level,int (* ih_fun)(void *),void * ih_arg)206 obio_intr_establish(int irq, int level, int (*ih_fun)(void *), void *ih_arg)
207 {
208
209 return extintr_establish(irq, level, ih_fun, ih_arg);
210 }
211
212 /*
213 * Deregister an interrupt handler.
214 */
215 void
obio_intr_disestablish(void * arg)216 obio_intr_disestablish(void *arg)
217 {
218
219 extintr_disestablish(arg);
220 }
221
222 /*
223 * on-board I/O bus space
224 */
225 #define OBIO_IOMEM_IO 0 /* space is i/o space */
226 #define OBIO_IOMEM_MEM 1 /* space is mem space */
227 #define OBIO_IOMEM_PCMCIA_IO 2 /* PCMCIA IO space */
228 #define OBIO_IOMEM_PCMCIA_MEM 3 /* PCMCIA Mem space */
229 #define OBIO_IOMEM_PCMCIA_ATT 4 /* PCMCIA Attr space */
230 #define OBIO_IOMEM_PCMCIA_8BIT 0x8000 /* PCMCIA BUS 8 BIT WIDTH */
231 #define OBIO_IOMEM_PCMCIA_IO8 \
232 (OBIO_IOMEM_PCMCIA_IO|OBIO_IOMEM_PCMCIA_8BIT)
233 #define OBIO_IOMEM_PCMCIA_MEM8 \
234 (OBIO_IOMEM_PCMCIA_MEM|OBIO_IOMEM_PCMCIA_8BIT)
235 #define OBIO_IOMEM_PCMCIA_ATT8 \
236 (OBIO_IOMEM_PCMCIA_ATT|OBIO_IOMEM_PCMCIA_8BIT)
237
238 int obio_iomem_map(void *v, bus_addr_t bpa, bus_size_t size, int flags,
239 bus_space_handle_t *bshp);
240 void obio_iomem_unmap(void *v, bus_space_handle_t bsh, bus_size_t size);
241 int obio_iomem_subregion(void *v, bus_space_handle_t bsh,
242 bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp);
243 int obio_iomem_alloc(void *v, bus_addr_t rstart, bus_addr_t rend,
244 bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
245 bus_addr_t *bpap, bus_space_handle_t *bshp);
246 void obio_iomem_free(void *v, bus_space_handle_t bsh, bus_size_t size);
247 paddr_t obio_iomem_mmap(void *v, bus_addr_t addr, off_t off, int prot,
248 int flags);
249
250 static int obio_iomem_add_mapping(bus_addr_t, bus_size_t, int,
251 bus_space_handle_t *);
252
253 static int
obio_iomem_add_mapping(bus_addr_t bpa,bus_size_t size,int type,bus_space_handle_t * bshp)254 obio_iomem_add_mapping(bus_addr_t bpa, bus_size_t size, int type,
255 bus_space_handle_t *bshp)
256 {
257 u_long pa, endpa;
258 vaddr_t va;
259 pt_entry_t *pte;
260 unsigned int m = 0;
261 int io_type = type & ~OBIO_IOMEM_PCMCIA_8BIT;
262
263 pa = sh3_trunc_page(bpa);
264 endpa = sh3_round_page(bpa + size);
265
266 #ifdef DIAGNOSTIC
267 if (endpa <= pa)
268 panic("obio_iomem_add_mapping: overflow");
269 #endif
270
271 va = uvm_km_alloc(kernel_map, endpa - pa, 0, UVM_KMF_VAONLY);
272 if (va == 0){
273 printf("obio_iomem_add_mapping: nomem\n");
274 return (ENOMEM);
275 }
276
277 *bshp = (bus_space_handle_t)(va + (bpa & PGOFSET));
278
279 #define MODE(t, s) \
280 ((t) & OBIO_IOMEM_PCMCIA_8BIT) ? \
281 _PG_PCMCIA_ ## s ## 8 : \
282 _PG_PCMCIA_ ## s ## 16
283 switch (io_type) {
284 default:
285 panic("unknown pcmcia space.");
286 /* NOTREACHED */
287 case OBIO_IOMEM_PCMCIA_IO:
288 m = MODE(type, IO);
289 break;
290 case OBIO_IOMEM_PCMCIA_MEM:
291 m = MODE(type, MEM);
292 break;
293 case OBIO_IOMEM_PCMCIA_ATT:
294 m = MODE(type, ATTR);
295 break;
296 }
297 #undef MODE
298
299 for (; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
300 pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, 0);
301 pte = __pmap_kpte_lookup(va);
302 KDASSERT(pte);
303 *pte |= m; /* PTEA PCMCIA assistant bit */
304 sh_tlb_update(0, va, *pte);
305 }
306
307 return (0);
308 }
309
310 int
obio_iomem_map(void * v,bus_addr_t bpa,bus_size_t size,int flags,bus_space_handle_t * bshp)311 obio_iomem_map(void *v, bus_addr_t bpa, bus_size_t size,
312 int flags, bus_space_handle_t *bshp)
313 {
314 bus_addr_t addr = SH3_PHYS_TO_P2SEG(bpa);
315 int error;
316
317 KASSERT((bpa & SH3_PHYS_MASK) == bpa);
318
319 if (bpa < 0x14000000 || bpa >= 0x1c000000) {
320 /* CS0,1,2,3,4,7 */
321 *bshp = (bus_space_handle_t)addr;
322 return (0);
323 }
324
325 /* CS5,6 */
326 error = obio_iomem_add_mapping(addr, size, (int)(u_long)v, bshp);
327
328 return (error);
329 }
330
331 void
obio_iomem_unmap(void * v,bus_space_handle_t bsh,bus_size_t size)332 obio_iomem_unmap(void *v, bus_space_handle_t bsh, bus_size_t size)
333 {
334 u_long va, endva;
335 bus_addr_t bpa;
336
337 if (bsh >= SH3_P2SEG_BASE && bsh <= SH3_P2SEG_END) {
338 /* maybe CS0,1,2,3,4,7 */
339 return;
340 }
341
342 /* CS5,6 */
343 va = sh3_trunc_page(bsh);
344 endva = sh3_round_page(bsh + size);
345
346 #ifdef DIAGNOSTIC
347 if (endva <= va)
348 panic("obio_io_unmap: overflow");
349 #endif
350
351 pmap_extract(pmap_kernel(), va, &bpa);
352 bpa += bsh & PGOFSET;
353
354 pmap_kremove(va, endva - va);
355
356 /*
357 * Free the kernel virtual mapping.
358 */
359 uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
360 }
361
362 int
obio_iomem_subregion(void * v,bus_space_handle_t bsh,bus_size_t offset,bus_size_t size,bus_space_handle_t * nbshp)363 obio_iomem_subregion(void *v, bus_space_handle_t bsh,
364 bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp)
365 {
366
367 *nbshp = bsh + offset;
368
369 return (0);
370 }
371
372 int
obio_iomem_alloc(void * v,bus_addr_t rstart,bus_addr_t rend,bus_size_t size,bus_size_t alignment,bus_size_t boundary,int flags,bus_addr_t * bpap,bus_space_handle_t * bshp)373 obio_iomem_alloc(void *v, bus_addr_t rstart, bus_addr_t rend,
374 bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
375 bus_addr_t *bpap, bus_space_handle_t *bshp)
376 {
377
378 *bshp = *bpap = rstart;
379
380 return (0);
381 }
382
383 void
obio_iomem_free(void * v,bus_space_handle_t bsh,bus_size_t size)384 obio_iomem_free(void *v, bus_space_handle_t bsh, bus_size_t size)
385 {
386
387 obio_iomem_unmap(v, bsh, size);
388 }
389
390 paddr_t
obio_iomem_mmap(void * v,bus_addr_t addr,off_t off,int prot,int flags)391 obio_iomem_mmap(void *v, bus_addr_t addr, off_t off, int prot, int flags)
392 {
393
394 return (paddr_t)-1;
395 }
396
397 /*
398 * on-board I/O bus space read/write
399 */
400 uint8_t obio_iomem_read_1(void *v, bus_space_handle_t bsh, bus_size_t offset);
401 uint16_t obio_iomem_read_2(void *v, bus_space_handle_t bsh, bus_size_t offset);
402 uint32_t obio_iomem_read_4(void *v, bus_space_handle_t bsh, bus_size_t offset);
403 void obio_iomem_read_multi_1(void *v, bus_space_handle_t bsh,
404 bus_size_t offset, uint8_t *addr, bus_size_t count);
405 void obio_iomem_read_multi_2(void *v, bus_space_handle_t bsh,
406 bus_size_t offset, uint16_t *addr, bus_size_t count);
407 void obio_iomem_read_multi_4(void *v, bus_space_handle_t bsh,
408 bus_size_t offset, uint32_t *addr, bus_size_t count);
409 void obio_iomem_read_region_1(void *v, bus_space_handle_t bsh,
410 bus_size_t offset, uint8_t *addr, bus_size_t count);
411 void obio_iomem_read_region_2(void *v, bus_space_handle_t bsh,
412 bus_size_t offset, uint16_t *addr, bus_size_t count);
413 void obio_iomem_read_region_4(void *v, bus_space_handle_t bsh,
414 bus_size_t offset, uint32_t *addr, bus_size_t count);
415 void obio_iomem_write_1(void *v, bus_space_handle_t bsh, bus_size_t offset,
416 uint8_t value);
417 void obio_iomem_write_2(void *v, bus_space_handle_t bsh, bus_size_t offset,
418 uint16_t value);
419 void obio_iomem_write_4(void *v, bus_space_handle_t bsh, bus_size_t offset,
420 uint32_t value);
421 void obio_iomem_write_multi_1(void *v, bus_space_handle_t bsh,
422 bus_size_t offset, const uint8_t *addr, bus_size_t count);
423 void obio_iomem_write_multi_2(void *v, bus_space_handle_t bsh,
424 bus_size_t offset, const uint16_t *addr, bus_size_t count);
425 void obio_iomem_write_multi_4(void *v, bus_space_handle_t bsh,
426 bus_size_t offset, const uint32_t *addr, bus_size_t count);
427 void obio_iomem_write_region_1(void *v, bus_space_handle_t bsh,
428 bus_size_t offset, const uint8_t *addr, bus_size_t count);
429 void obio_iomem_write_region_2(void *v, bus_space_handle_t bsh,
430 bus_size_t offset, const uint16_t *addr, bus_size_t count);
431 void obio_iomem_write_region_4(void *v, bus_space_handle_t bsh,
432 bus_size_t offset, const uint32_t *addr, bus_size_t count);
433 void obio_iomem_set_multi_1(void *v, bus_space_handle_t bsh, bus_size_t offset,
434 uint8_t val, bus_size_t count);
435 void obio_iomem_set_multi_2(void *v, bus_space_handle_t bsh, bus_size_t offset,
436 uint16_t val, bus_size_t count);
437 void obio_iomem_set_multi_4(void *v, bus_space_handle_t bsh, bus_size_t offset,
438 uint32_t val, bus_size_t count);
439 void obio_iomem_set_region_1(void *v, bus_space_handle_t bsh,
440 bus_size_t offset, uint8_t val, bus_size_t count);
441 void obio_iomem_set_region_2(void *v, bus_space_handle_t bsh,
442 bus_size_t offset, uint16_t val, bus_size_t count);
443 void obio_iomem_set_region_4(void *v, bus_space_handle_t bsh,
444 bus_size_t offset, uint32_t val, bus_size_t count);
445 void obio_iomem_copy_region_1(void *v, bus_space_handle_t h1, bus_size_t o1,
446 bus_space_handle_t h2, bus_size_t o2, bus_size_t count);
447 void obio_iomem_copy_region_2(void *v, bus_space_handle_t h1, bus_size_t o1,
448 bus_space_handle_t h2, bus_size_t o2, bus_size_t count);
449 void obio_iomem_copy_region_4(void *v, bus_space_handle_t h1, bus_size_t o1,
450 bus_space_handle_t h2, bus_size_t o2, bus_size_t count);
451
452 struct _bus_space obio_bus_io =
453 {
454 .bs_cookie = (void *)OBIO_IOMEM_PCMCIA_IO,
455
456 .bs_map = obio_iomem_map,
457 .bs_unmap = obio_iomem_unmap,
458 .bs_subregion = obio_iomem_subregion,
459
460 .bs_alloc = obio_iomem_alloc,
461 .bs_free = obio_iomem_free,
462
463 .bs_mmap = obio_iomem_mmap,
464
465 .bs_r_1 = obio_iomem_read_1,
466 .bs_r_2 = obio_iomem_read_2,
467 .bs_r_4 = obio_iomem_read_4,
468
469 .bs_rm_1 = obio_iomem_read_multi_1,
470 .bs_rm_2 = obio_iomem_read_multi_2,
471 .bs_rm_4 = obio_iomem_read_multi_4,
472
473 .bs_rr_1 = obio_iomem_read_region_1,
474 .bs_rr_2 = obio_iomem_read_region_2,
475 .bs_rr_4 = obio_iomem_read_region_4,
476
477 .bs_w_1 = obio_iomem_write_1,
478 .bs_w_2 = obio_iomem_write_2,
479 .bs_w_4 = obio_iomem_write_4,
480
481 .bs_wm_1 = obio_iomem_write_multi_1,
482 .bs_wm_2 = obio_iomem_write_multi_2,
483 .bs_wm_4 = obio_iomem_write_multi_4,
484
485 .bs_wr_1 = obio_iomem_write_region_1,
486 .bs_wr_2 = obio_iomem_write_region_2,
487 .bs_wr_4 = obio_iomem_write_region_4,
488
489 .bs_sm_1 = obio_iomem_set_multi_1,
490 .bs_sm_2 = obio_iomem_set_multi_2,
491 .bs_sm_4 = obio_iomem_set_multi_4,
492
493 .bs_sr_1 = obio_iomem_set_region_1,
494 .bs_sr_2 = obio_iomem_set_region_2,
495 .bs_sr_4 = obio_iomem_set_region_4,
496
497 .bs_c_1 = obio_iomem_copy_region_1,
498 .bs_c_2 = obio_iomem_copy_region_2,
499 .bs_c_4 = obio_iomem_copy_region_4,
500 };
501
502 struct _bus_space obio_bus_mem =
503 {
504 .bs_cookie = (void *)OBIO_IOMEM_PCMCIA_MEM,
505
506 .bs_map = obio_iomem_map,
507 .bs_unmap = obio_iomem_unmap,
508 .bs_subregion = obio_iomem_subregion,
509
510 .bs_alloc = obio_iomem_alloc,
511 .bs_free = obio_iomem_free,
512
513 .bs_r_1 = obio_iomem_read_1,
514 .bs_r_2 = obio_iomem_read_2,
515 .bs_r_4 = obio_iomem_read_4,
516
517 .bs_rm_1 = obio_iomem_read_multi_1,
518 .bs_rm_2 = obio_iomem_read_multi_2,
519 .bs_rm_4 = obio_iomem_read_multi_4,
520
521 .bs_rr_1 = obio_iomem_read_region_1,
522 .bs_rr_2 = obio_iomem_read_region_2,
523 .bs_rr_4 = obio_iomem_read_region_4,
524
525 .bs_w_1 = obio_iomem_write_1,
526 .bs_w_2 = obio_iomem_write_2,
527 .bs_w_4 = obio_iomem_write_4,
528
529 .bs_wm_1 = obio_iomem_write_multi_1,
530 .bs_wm_2 = obio_iomem_write_multi_2,
531 .bs_wm_4 = obio_iomem_write_multi_4,
532
533 .bs_wr_1 = obio_iomem_write_region_1,
534 .bs_wr_2 = obio_iomem_write_region_2,
535 .bs_wr_4 = obio_iomem_write_region_4,
536
537 .bs_sm_1 = obio_iomem_set_multi_1,
538 .bs_sm_2 = obio_iomem_set_multi_2,
539 .bs_sm_4 = obio_iomem_set_multi_4,
540
541 .bs_sr_1 = obio_iomem_set_region_1,
542 .bs_sr_2 = obio_iomem_set_region_2,
543 .bs_sr_4 = obio_iomem_set_region_4,
544
545 .bs_c_1 = obio_iomem_copy_region_1,
546 .bs_c_2 = obio_iomem_copy_region_2,
547 .bs_c_4 = obio_iomem_copy_region_4,
548 };
549
550 /* read */
551 uint8_t
obio_iomem_read_1(void * v,bus_space_handle_t bsh,bus_size_t offset)552 obio_iomem_read_1(void *v, bus_space_handle_t bsh, bus_size_t offset)
553 {
554
555 return *(volatile uint8_t *)(bsh + offset);
556 }
557
558 uint16_t
obio_iomem_read_2(void * v,bus_space_handle_t bsh,bus_size_t offset)559 obio_iomem_read_2(void *v, bus_space_handle_t bsh, bus_size_t offset)
560 {
561
562 return *(volatile uint16_t *)(bsh + offset);
563 }
564
565 uint32_t
obio_iomem_read_4(void * v,bus_space_handle_t bsh,bus_size_t offset)566 obio_iomem_read_4(void *v, bus_space_handle_t bsh, bus_size_t offset)
567 {
568
569 return *(volatile uint32_t *)(bsh + offset);
570 }
571
572 void
obio_iomem_read_multi_1(void * v,bus_space_handle_t bsh,bus_size_t offset,uint8_t * addr,bus_size_t count)573 obio_iomem_read_multi_1(void *v, bus_space_handle_t bsh,
574 bus_size_t offset, uint8_t *addr, bus_size_t count)
575 {
576 volatile uint8_t *p = (void *)(bsh + offset);
577
578 while (count--) {
579 *addr++ = *p;
580 }
581 }
582
583 void
obio_iomem_read_multi_2(void * v,bus_space_handle_t bsh,bus_size_t offset,uint16_t * addr,bus_size_t count)584 obio_iomem_read_multi_2(void *v, bus_space_handle_t bsh,
585 bus_size_t offset, uint16_t *addr, bus_size_t count)
586 {
587 volatile uint16_t *p = (void *)(bsh + offset);
588
589 while (count--) {
590 *addr++ = *p;
591 }
592 }
593
594 void
obio_iomem_read_multi_4(void * v,bus_space_handle_t bsh,bus_size_t offset,uint32_t * addr,bus_size_t count)595 obio_iomem_read_multi_4(void *v, bus_space_handle_t bsh,
596 bus_size_t offset, uint32_t *addr, bus_size_t count)
597 {
598 volatile uint32_t *p = (void *)(bsh + offset);
599
600 while (count--) {
601 *addr++ = *p;
602 }
603 }
604
605 void
obio_iomem_read_region_1(void * v,bus_space_handle_t bsh,bus_size_t offset,uint8_t * addr,bus_size_t count)606 obio_iomem_read_region_1(void *v, bus_space_handle_t bsh,
607 bus_size_t offset, uint8_t *addr, bus_size_t count)
608 {
609 volatile uint8_t *p = (void *)(bsh + offset);
610
611 while (count--) {
612 *addr++ = *p++;
613 }
614 }
615
616 void
obio_iomem_read_region_2(void * v,bus_space_handle_t bsh,bus_size_t offset,uint16_t * addr,bus_size_t count)617 obio_iomem_read_region_2(void *v, bus_space_handle_t bsh,
618 bus_size_t offset, uint16_t *addr, bus_size_t count)
619 {
620 volatile uint16_t *p = (void *)(bsh + offset);
621
622 while (count--) {
623 *addr++ = *p++;
624 }
625 }
626
627 void
obio_iomem_read_region_4(void * v,bus_space_handle_t bsh,bus_size_t offset,uint32_t * addr,bus_size_t count)628 obio_iomem_read_region_4(void *v, bus_space_handle_t bsh,
629 bus_size_t offset, uint32_t *addr, bus_size_t count)
630 {
631 volatile uint32_t *p = (void *)(bsh + offset);
632
633 while (count--) {
634 *addr++ = *p++;
635 }
636 }
637
638 /* write */
639 void
obio_iomem_write_1(void * v,bus_space_handle_t bsh,bus_size_t offset,uint8_t value)640 obio_iomem_write_1(void *v, bus_space_handle_t bsh, bus_size_t offset,
641 uint8_t value)
642 {
643
644 *(volatile uint8_t *)(bsh + offset) = value;
645 }
646
647 void
obio_iomem_write_2(void * v,bus_space_handle_t bsh,bus_size_t offset,uint16_t value)648 obio_iomem_write_2(void *v, bus_space_handle_t bsh, bus_size_t offset,
649 uint16_t value)
650 {
651
652 *(volatile uint16_t *)(bsh + offset) = value;
653 }
654
655 void
obio_iomem_write_4(void * v,bus_space_handle_t bsh,bus_size_t offset,uint32_t value)656 obio_iomem_write_4(void *v, bus_space_handle_t bsh, bus_size_t offset,
657 uint32_t value)
658 {
659
660 *(volatile uint32_t *)(bsh + offset) = value;
661 }
662
663 void
obio_iomem_write_multi_1(void * v,bus_space_handle_t bsh,bus_size_t offset,const uint8_t * addr,bus_size_t count)664 obio_iomem_write_multi_1(void *v, bus_space_handle_t bsh,
665 bus_size_t offset, const uint8_t *addr, bus_size_t count)
666 {
667 volatile uint8_t *p = (void *)(bsh + offset);
668
669 while (count--) {
670 *p = *addr++;
671 }
672 }
673
674 void
obio_iomem_write_multi_2(void * v,bus_space_handle_t bsh,bus_size_t offset,const uint16_t * addr,bus_size_t count)675 obio_iomem_write_multi_2(void *v, bus_space_handle_t bsh,
676 bus_size_t offset, const uint16_t *addr, bus_size_t count)
677 {
678 volatile uint16_t *p = (void *)(bsh + offset);
679
680 while (count--) {
681 *p = *addr++;
682 }
683 }
684
685 void
obio_iomem_write_multi_4(void * v,bus_space_handle_t bsh,bus_size_t offset,const uint32_t * addr,bus_size_t count)686 obio_iomem_write_multi_4(void *v, bus_space_handle_t bsh,
687 bus_size_t offset, const uint32_t *addr, bus_size_t count)
688 {
689 volatile uint32_t *p = (void *)(bsh + offset);
690
691 while (count--) {
692 *p = *addr++;
693 }
694 }
695
696 void
obio_iomem_write_region_1(void * v,bus_space_handle_t bsh,bus_size_t offset,const uint8_t * addr,bus_size_t count)697 obio_iomem_write_region_1(void *v, bus_space_handle_t bsh,
698 bus_size_t offset, const uint8_t *addr, bus_size_t count)
699 {
700 volatile uint8_t *p = (void *)(bsh + offset);
701
702 while (count--) {
703 *p++ = *addr++;
704 }
705 }
706
707 void
obio_iomem_write_region_2(void * v,bus_space_handle_t bsh,bus_size_t offset,const uint16_t * addr,bus_size_t count)708 obio_iomem_write_region_2(void *v, bus_space_handle_t bsh,
709 bus_size_t offset, const uint16_t *addr, bus_size_t count)
710 {
711 volatile uint16_t *p = (void *)(bsh + offset);
712
713 while (count--) {
714 *p++ = *addr++;
715 }
716 }
717
718 void
obio_iomem_write_region_4(void * v,bus_space_handle_t bsh,bus_size_t offset,const uint32_t * addr,bus_size_t count)719 obio_iomem_write_region_4(void *v, bus_space_handle_t bsh,
720 bus_size_t offset, const uint32_t *addr, bus_size_t count)
721 {
722 volatile uint32_t *p = (void *)(bsh + offset);
723
724 while (count--) {
725 *p++ = *addr++;
726 }
727 }
728
729 void
obio_iomem_set_multi_1(void * v,bus_space_handle_t bsh,bus_size_t offset,uint8_t val,bus_size_t count)730 obio_iomem_set_multi_1(void *v, bus_space_handle_t bsh,
731 bus_size_t offset, uint8_t val, bus_size_t count)
732 {
733 volatile uint8_t *p = (void *)(bsh + offset);
734
735 while (count--) {
736 *p = val;
737 }
738 }
739
740 void
obio_iomem_set_multi_2(void * v,bus_space_handle_t bsh,bus_size_t offset,uint16_t val,bus_size_t count)741 obio_iomem_set_multi_2(void *v, bus_space_handle_t bsh,
742 bus_size_t offset, uint16_t val, bus_size_t count)
743 {
744 volatile uint16_t *p = (void *)(bsh + offset);
745
746 while (count--) {
747 *p = val;
748 }
749 }
750
751 void
obio_iomem_set_multi_4(void * v,bus_space_handle_t bsh,bus_size_t offset,uint32_t val,bus_size_t count)752 obio_iomem_set_multi_4(void *v, bus_space_handle_t bsh,
753 bus_size_t offset, uint32_t val, bus_size_t count)
754 {
755 volatile uint32_t *p = (void *)(bsh + offset);
756
757 while (count--) {
758 *p = val;
759 }
760 }
761
762 void
obio_iomem_set_region_1(void * v,bus_space_handle_t bsh,bus_size_t offset,uint8_t val,bus_size_t count)763 obio_iomem_set_region_1(void *v, bus_space_handle_t bsh,
764 bus_size_t offset, uint8_t val, bus_size_t count)
765 {
766 volatile uint8_t *addr = (void *)(bsh + offset);
767
768 while (count--) {
769 *addr++ = val;
770 }
771 }
772
773 void
obio_iomem_set_region_2(void * v,bus_space_handle_t bsh,bus_size_t offset,uint16_t val,bus_size_t count)774 obio_iomem_set_region_2(void *v, bus_space_handle_t bsh,
775 bus_size_t offset, uint16_t val, bus_size_t count)
776 {
777 volatile uint16_t *addr = (void *)(bsh + offset);
778
779 while (count--) {
780 *addr++ = val;
781 }
782 }
783
784 void
obio_iomem_set_region_4(void * v,bus_space_handle_t bsh,bus_size_t offset,uint32_t val,bus_size_t count)785 obio_iomem_set_region_4(void *v, bus_space_handle_t bsh,
786 bus_size_t offset, uint32_t val, bus_size_t count)
787 {
788 volatile uint32_t *addr = (void *)(bsh + offset);
789
790 while (count--) {
791 *addr++ = val;
792 }
793 }
794
795 void
obio_iomem_copy_region_1(void * v,bus_space_handle_t h1,bus_size_t o1,bus_space_handle_t h2,bus_size_t o2,bus_size_t count)796 obio_iomem_copy_region_1(void *v, bus_space_handle_t h1, bus_size_t o1,
797 bus_space_handle_t h2, bus_size_t o2, bus_size_t count)
798 {
799 volatile uint8_t *addr1 = (void *)(h1 + o1);
800 volatile uint8_t *addr2 = (void *)(h2 + o2);
801
802 if (addr1 >= addr2) { /* src after dest: copy forward */
803 while (count--) {
804 *addr2++ = *addr1++;
805 }
806 } else { /* dest after src: copy backwards */
807 addr1 += count - 1;
808 addr2 += count - 1;
809 while (count--) {
810 *addr2-- = *addr1--;
811 }
812 }
813 }
814
815 void
obio_iomem_copy_region_2(void * v,bus_space_handle_t h1,bus_size_t o1,bus_space_handle_t h2,bus_size_t o2,bus_size_t count)816 obio_iomem_copy_region_2(void *v, bus_space_handle_t h1, bus_size_t o1,
817 bus_space_handle_t h2, bus_size_t o2, bus_size_t count)
818 {
819 volatile uint16_t *addr1 = (void *)(h1 + o1);
820 volatile uint16_t *addr2 = (void *)(h2 + o2);
821
822 if (addr1 >= addr2) { /* src after dest: copy forward */
823 while (count--) {
824 *addr2++ = *addr1++;
825 }
826 } else { /* dest after src: copy backwards */
827 addr1 += count - 1;
828 addr2 += count - 1;
829 while (count--) {
830 *addr2-- = *addr1--;
831 }
832 }
833 }
834
835 void
obio_iomem_copy_region_4(void * v,bus_space_handle_t h1,bus_size_t o1,bus_space_handle_t h2,bus_size_t o2,bus_size_t count)836 obio_iomem_copy_region_4(void *v, bus_space_handle_t h1, bus_size_t o1,
837 bus_space_handle_t h2, bus_size_t o2, bus_size_t count)
838 {
839 volatile uint32_t *addr1 = (void *)(h1 + o1);
840 volatile uint32_t *addr2 = (void *)(h2 + o2);
841
842 if (addr1 >= addr2) { /* src after dest: copy forward */
843 while (count--) {
844 *addr2++ = *addr1++;
845 }
846 } else { /* dest after src: copy backwards */
847 addr1 += count - 1;
848 addr2 += count - 1;
849 while (count--) {
850 *addr2-- = *addr1--;
851 }
852 }
853 }
854