1 /* $NetBSD: globals.h,v 1.21 2015/09/30 14:14:32 phx Exp $ */
2 
3 #ifdef DEBUG
4 #define   DPRINTF(x)          printf x
5 #else
6 #define   DPRINTF(x)
7 #endif
8 
9 /* clock feed */
10 #ifndef EXT_CLK_FREQ
11 #define EXT_CLK_FREQ          33333333  /* external clock (PCI clock) */
12 #endif
13 
14 /* brd type */
15 extern int brdtype;
16 #define BRD_SANDPOINTX2                 2
17 #define BRD_SANDPOINTX3                 3
18 #define BRD_ENCOREPP1                   10
19 #define BRD_KUROBOX           100
20 #define BRD_QNAPTS            101
21 #define BRD_SYNOLOGY                    102
22 #define BRD_STORCENTER                  103
23 #define BRD_DLINKDSM                    104
24 #define BRD_NH230NAS                    105
25 #define BRD_KUROBOXT4                   106
26 #define BRD_UNKNOWN           -1
27 
28 struct brdprop {
29           const char *family;
30           const char *verbose;
31           int brdtype;
32           uint32_t extclk;
33           char *consname;
34           int consport;
35           int consspeed;
36           void (*setup)(struct brdprop *);
37           void (*brdfix)(struct brdprop *);
38           void (*pcifix)(struct brdprop *);
39           void (*launch)(struct brdprop *);
40           void (*reset)(void);
41 };
42 
43 extern uint32_t cpuclock, busclock;
44 
45 /* board specific support code */
46 struct brdprop *brd_lookup(int);
47 int get_drive_config(int);
48 int tstchar(void);
49 #ifdef DEBUG
50 void sat_write(char *, int);
51 int sat_getch(void);
52 int sat_tstch(void);
53 #endif
54 void read_mac_from_flash(uint8_t *);
55 
56 /* PPC processor ctl */
57 void __syncicache(void *, size_t);
58 
59 /* i/o access */
60 void out8(unsigned, unsigned);
61 unsigned in8(unsigned);
62 void out16rb(unsigned, unsigned);
63 void out32rb(unsigned, unsigned);
64 unsigned in16rb(unsigned);
65 unsigned in32rb(unsigned);
66 void iohtole16(unsigned, unsigned);
67 void iohtole32(unsigned, unsigned);
68 unsigned iole32toh(unsigned);
69 unsigned iole16toh(unsigned);
70 
71 /* far call would never return */
72 void run(void *, void *, void *, void *, void *);
73 
74 /* micro second precision delay */
75 void delay(unsigned);
76 
77 /* PCI stuff */
78 struct pcidev {
79           unsigned bdf;       /* bus.dev.func */
80           unsigned pvd;       /* device ID */
81           void *drv;          /* driver */
82 };
83 extern struct pcidev lata[2];
84 extern struct pcidev lnif[2];
85 extern struct pcidev lusb[3];
86 extern int nata, nnif, nusb;
87 
88 void  pcisetup(void);
89 void  pcifixup(void);
90 void  launchfixup(void);
91 unsigned pcimaketag(int, int, int);
92 void  pcidecomposetag(unsigned, int *, int *, int *);
93 int   pcifinddev(unsigned, unsigned, unsigned *);
94 int   pcilookup(unsigned, struct pcidev *, int);
95 unsigned pcicfgread(unsigned, int);
96 void  pcicfgwrite(unsigned, int, unsigned);
97 
98 #define PCI_ID_REG                      0x00
99 #define  PCI_VENDOR(id)                           ((id) & 0xffff)
100 #define  PCI_PRODUCT(id)                (((id) >> 16) & 0xffff)
101 #define  PCI_VENDOR_INVALID             0xffff
102 #define  PCI_DEVICE(v,p)                ((v) | ((p) << 16))
103 #define PCI_COMMAND_STATUS_REG                    0x04
104 #define PCI_CLASS_REG                             0x08
105 #define  PCI_CLASS(v)                             (((v) >> 16) & 0xffff)
106 #define  PCI_SUBCLASS(v)                (((v) >> 16) & 0xff)
107 #define  PCI_INTERFACE(v)               (((v) & 0xff00) >> 8)
108 #define  PCI_REVISION(v)                ((v) & 0xff)
109 #define  PCI_CLASS_PPB                            0x0604
110 #define  PCI_CLASS_ETH                            0x0200
111 #define  PCI_CLASS_SCSI                           0x0100
112 #define  PCI_CLASS_IDE                            0x0101
113 #define  PCI_CLASS_RAID                           0x0104
114 #define  PCI_CLASS_SATA                           0x0106
115 #define  PCI_CLASS_MISCSTORAGE                    0x0180
116 #define  PCI_CLASS_USB                            0x0c03
117 #define PCI_BHLC_REG                              0x0c
118 #define  PCI_HDRTYPE_TYPE(r)            (((r) >> 16) & 0x7f)
119 #define  PCI_HDRTYPE_MULTIFN(r)                   ((r) & (0x80 << 16))
120 
121 /*
122  * "Map B" layout
123  *
124  * practice direct mode configuration scheme with CONFIG_ADDR
125  * (0xfec0'0000) and CONFIG_DATA (0xfee0'0000).
126  */
127 #define PCI_MEMBASE 0x80000000          /* PCI memory space */
128 #define PCI_MEMLIMIT          0xfbffffff          /* EUMB is next to this */
129 #define PCI_IOBASE  0x00001000          /* reserves room for southbridge */
130 #define PCI_IOLIMIT 0x000fffff
131 #define PCI_XIOBASE 0xfe000000          /* ISA/PCI io space */
132 #define CONFIG_ADDR 0xfec00000
133 #define CONFIG_DATA 0xfee00000
134 
135 /* cache ops */
136 void _wb(uint32_t, uint32_t);
137 void _wbinv(uint32_t, uint32_t);
138 void _inv(uint32_t, uint32_t);
139 
140 /* parsing */
141 uint32_t read_hex(const char *);
142 
143 /* heap */
144 void *allocaligned(size_t, size_t);
145 
146 /* NIF support */
147 int net_open(struct open_file *, ...);
148 int net_close(struct open_file *);
149 int net_strategy(void *, int, daddr_t, size_t, void *, size_t *);
150 
151 int netif_init(void *);
152 void netif_shutdown_all(void);
153 int netif_open(void *);
154 int netif_close(int);
155 
156 #define NIF_DECL(xxx) \
157     int xxx ## _match(unsigned, void *); \
158     void * xxx ## _init(unsigned, void *); \
159     int xxx ## _send(void *, char *, unsigned); \
160     int xxx ## _recv(void *, char *, unsigned, unsigned); \
161     void xxx ## _shutdown(void *)
162 
163 NIF_DECL(fxp);
164 NIF_DECL(tlp);
165 NIF_DECL(rge);
166 NIF_DECL(skg);
167 NIF_DECL(stg);
168 
169 /* DSK support */
170 #define MAX_UNITS 4
171 
172 struct disk {
173           char xname[8];
174           void *dvops;
175           unsigned unitchan;
176           unsigned unittag;
177           uint16_t ident[128];
178           uint64_t nsect;
179           uint64_t first;
180           void *dlabel;
181           int part;
182           void *fsops;
183           int (*lba_read)(struct disk *, int64_t, int, void *);
184 };
185 
186 int dskdv_init(void *);
187 int dlabel_valid(int);
188 int dsk_open(struct open_file *, ...);
189 int dsk_close(struct open_file *);
190 int dsk_strategy(void *, int, daddr_t, size_t, void *, size_t *);
191 struct fs_ops *dsk_fsops(struct open_file *);
192 
193 #define DSK_DECL(xxx) \
194     int xxx ## _match(unsigned, void *); \
195     void * xxx ## _init(unsigned, void *)
196 
197 DSK_DECL(pciide);
198 DSK_DECL(siisata);
199 
200 extern int sata_delay[4];
201 
202 /* status */
203 #define ATA_STS_BUSY                    0x80
204 #define ATA_STS_DRDY                    0x40
205 #define ATA_STS_ERR                     0x01
206 /* command */
207 #define ATA_CMD_CHKPWR                  0xe5
208 #define ATA_CMD_IDENT                   0xec
209 #define ATA_CMD_IDLE                    0xe3
210 #define ATA_CMD_READ                    0x20
211 #define ATA_CMD_READ_EXT      0x24
212 #define ATA_CMD_SETF                    0xef
213 #define ATA_CMD_STANDBY                 0xe2
214 /* device */
215 #define ATA_DEV_LBA           0xe0
216 #define ATA_DEV_OBS           0x90
217 /* control */
218 #define ATA_DREQ              0x08
219 #define ATA_SRST              0x04
220 /* power state */
221 #define ATA_PWR_ACTIVE                  0xff
222 #define ATA_PWR_IDLE                    0x80
223 #define ATA_PWR_STANDBY                 0x00
224 
225 #define ATA_XFER              0x03
226 #define XFER_PIO4             0x0c
227 #define XFER_PIO0             0x08
228 
229 struct dvata_chan {
230           uint32_t cmd, ctl, alt, dma;
231 };
232 #define _DAT        0         /* RW */
233 #define _ERR        1         /* R */
234 #define _FEA        1         /* W */
235 #define _NSECT      2         /* RW */
236 #define _LBAL       3         /* RW */
237 #define _LBAM       4         /* RW */
238 #define _LBAH       5         /* RW */
239 #define _DEV        6         /* W */
240 #define _STS        7         /* R */
241 #define _CMD        7         /* W */
242 
243 struct dkdev_ata {
244           unsigned tag;
245           uint32_t bar[6];
246           struct dvata_chan chan[4];
247           int presense[4];
248           char *iobuf;
249 };
250 
251 int spinwait_unbusy(struct dkdev_ata *, int, int, const char **);
252 int perform_atareset(struct dkdev_ata *, int);
253 void wakeup_drive(struct dkdev_ata *, int);
254 int atachkpwr(struct dkdev_ata *, int);
255