1 /* $NetBSD: xpcmd.c,v 1.1 2022/06/10 21:42:23 tsutsui Exp $ */
2
3 /*
4 * Copyright (c) 2018 Yosuke Sugahara. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/types.h>
29
30 #if defined(_KERNEL)
31
32 #include <sys/param.h>
33
34 #include <luna68k/dev/xpbusvar.h>
35 #include <luna68k/dev/xpcmd.h>
36 #include <luna68k/dev/xplx/xplxdefs.h>
37 #else
38
39 #include <stdint.h>
40 #include "xpbusvar.h"
41 #include "xpcmd.h"
42 #include "xplx/xplxdefs.h"
43
44 #endif
45
46 enum {
47 READY = 0,
48 CMD = 1,
49 RESULT = 2,
50 };
51
52 static int
xp_waitfor_ready(int xplx_devid,int timeout)53 xp_waitfor_ready(int xplx_devid, int timeout)
54 {
55 uint16_t addr = XPLX_VAR_BASE + xplx_devid * 16;
56
57 do {
58 int r;
59 r = xp_readmem8(addr + READY);
60 if (r)
61 return 1;
62 delay(100);
63 } while (timeout--);
64 return 0;
65 }
66
67 int
xp_cmd_nowait(int xplx_devid,uint8_t cmd)68 xp_cmd_nowait(int xplx_devid, uint8_t cmd)
69 {
70 uint8_t r;
71
72 uint16_t addr = XPLX_VAR_BASE + xplx_devid * 16;
73 r = xp_waitfor_ready(xplx_devid, 1000);
74 if (r == 0)
75 return 0;
76 xp_writemem8(addr + RESULT, 0);
77 xp_writemem8(addr + CMD, cmd);
78 return XPLX_R_OK;
79 }
80
81 int
xp_cmd(int xplx_devid,uint8_t cmd)82 xp_cmd(int xplx_devid, uint8_t cmd)
83 {
84 uint8_t rv;
85 uint8_t r;
86 uint16_t addr = XPLX_VAR_BASE + xplx_devid * 16;
87
88 r = xp_waitfor_ready(xplx_devid, 1000);
89 if (r == 0)
90 return 0;
91 xp_writemem8(addr + RESULT, 0);
92 xp_writemem8(addr + CMD, cmd);
93 while ((rv = xp_readmem8(addr + RESULT)) == 0)
94 continue;
95 return rv;
96 }
97