1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/* $OpenBSD: com_pci.c,v 1.1 2020/03/06 08:39:34 patrick Exp $ */
/*
* Copyright (c) 2020 Patrick Wildt <patrick@blueri.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/selinfo.h>
#include <sys/tty.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/ic/comreg.h>
#include <dev/ic/comvar.h>
#include <dev/ic/ns16550reg.h>
#define com_usr 31 /* Synopsys DesignWare UART */
/* Intel Low Power Subsystem */
#define LPSS_CLK 0x200
#define LPSS_CLK_GATE (1 << 0)
#define LPSS_CLK_MDIV_SHIFT 1
#define LPSS_CLK_MDIV_MASK 0x3fff
#define LPSS_CLK_NDIV_SHIFT 16
#define LPSS_CLK_NDIV_MASK 0x3fff
#define LPSS_CLK_UPDATE (1U << 31)
#define LPSS_RESETS 0x204
#define LPSS_RESETS_FUNC (3 << 0)
#define LPSS_RESETS_IDMA (1 << 2)
#define LPSS_ACTIVELTR 0x210
#define LPSS_IDLELTR 0x214
#define LPSS_LTR_VALUE_MASK (0x3ff << 0)
#define LPSS_LTR_SCALE_MASK (0x3 << 10)
#define LPSS_LTR_SCALE_1US (2 << 10)
#define LPSS_LTR_SCALE_32US (3 << 10)
#define LPSS_LTR_REQ (1 << 15)
#define LPSS_SSP 0x220
#define LPSS_SSP_DIS_DMA_FIN (1 << 0)
#define LPSS_REMAP_ADDR 0x240
#define LPSS_CAPS 0x2fc
#define LPSS_CAPS_TYPE_I2C (0 << 4)
#define LPSS_CAPS_TYPE_UART (1 << 4)
#define LPSS_CAPS_TYPE_SPI (2 << 4)
#define LPSS_CAPS_TYPE_MASK (0xf << 4)
#define LPSS_CAPS_NO_IDMA (1 << 8)
#define LPSS_REG_OFF 0x200
#define LPSS_REG_SIZE 0x100
#define LPSS_REG_NUM (LPSS_REG_SIZE / sizeof(uint32_t))
#define HREAD4(sc, reg) \
(bus_space_read_4((sc)->sc.sc_iot, (sc)->sc.sc_ioh, (reg)))
#define HWRITE4(sc, reg, val) \
bus_space_write_4((sc)->sc.sc_iot, (sc)->sc.sc_ioh, (reg), (val))
#define HSET4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
#define HCLR4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))
int com_pci_match(struct device *, void *, void *);
void com_pci_attach(struct device *, struct device *, void *);
int com_pci_detach(struct device *, int);
int com_pci_activate(struct device *, int);
int com_pci_intr_designware(void *);
struct com_pci_softc {
struct com_softc sc;
pci_chipset_tag_t sc_pc;
pcireg_t sc_id;
bus_size_t sc_ios;
void *sc_ih;
uint32_t sc_priv[LPSS_REG_NUM];
};
struct cfattach com_pci_ca = {
sizeof(struct com_pci_softc), com_pci_match,
com_pci_attach, com_pci_detach, com_pci_activate,
};
const struct pci_matchid com_pci_ids[] = {
{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_APOLLOLAKE_UART_1 },
{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_APOLLOLAKE_UART_2 },
{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_APOLLOLAKE_UART_3 },
{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_APOLLOLAKE_UART_4 },
};
int
com_pci_match(struct device *parent, void *match, void *aux)
{
return (pci_matchbyid(aux, com_pci_ids, nitems(com_pci_ids)));
}
void
com_pci_attach(struct device *parent, struct device *self, void *aux)
{
struct com_pci_softc *sc = (struct com_pci_softc *)self;
struct pci_attach_args *pa = aux;
pci_intr_handle_t ih;
const char *intrstr;
uint64_t freq, m, n;
uint32_t caps;
sc->sc_pc = pa->pa_pc;
sc->sc_id = pa->pa_id;
sc->sc.sc_frequency = COM_FREQ;
sc->sc.sc_uarttype = COM_UART_16550;
sc->sc.sc_reg_width = 4;
sc->sc.sc_reg_shift = 2;
pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_MEM_TYPE_64BIT, 0,
&sc->sc.sc_iot, &sc->sc.sc_ioh, &sc->sc.sc_iobase, &sc->sc_ios, 0)) {
printf(": can't map mem space\n");
return;
}
/*
* Once we are adding non-Intel and non-LPSS device it will make
* sense to add a second table and use pci_matchbyid().
*/
if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
caps = HREAD4(sc, LPSS_CAPS);
if ((caps & LPSS_CAPS_TYPE_MASK) != LPSS_CAPS_TYPE_UART) {
bus_space_unmap(sc->sc.sc_iot, sc->sc.sc_ioh,
sc->sc_ios);
printf(": not a UART\n");
return;
}
HWRITE4(sc, LPSS_RESETS, 0);
HWRITE4(sc, LPSS_RESETS, LPSS_RESETS_FUNC | LPSS_RESETS_IDMA);
HWRITE4(sc, LPSS_REMAP_ADDR, sc->sc.sc_iobase);
/* 100 MHz base clock */
freq = 100 * 1000 * 1000;
m = n = HREAD4(sc, LPSS_CLK);
m = (m >> LPSS_CLK_MDIV_SHIFT) & LPSS_CLK_MDIV_MASK;
n = (n >> LPSS_CLK_NDIV_SHIFT) & LPSS_CLK_NDIV_MASK;
if (m && n) {
freq *= m;
freq /= n;
}
sc->sc.sc_frequency = freq;
}
if (pci_intr_map_msi(pa, &ih) != 0 && pci_intr_map(pa, &ih) != 0) {
bus_space_unmap(sc->sc.sc_iot, sc->sc.sc_ioh, sc->sc_ios);
printf(": unable to map interrupt\n");
return;
}
intrstr = pci_intr_string(pa->pa_pc, ih);
sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_TTY,
com_pci_intr_designware, &sc->sc, sc->sc.sc_dev.dv_xname);
if (sc->sc_ih == NULL) {
bus_space_unmap(sc->sc.sc_iot, sc->sc.sc_ioh, sc->sc_ios);
printf(": can't establish interrupt");
if (intrstr != NULL)
printf(" at %s", intrstr);
printf("\n");
return;
}
com_attach_subr(&sc->sc);
}
int
com_pci_detach(struct device *self, int flags)
{
struct com_pci_softc *sc = (struct com_pci_softc *)self;
int rv;
rv = com_detach(self, flags);
if (rv != 0)
return (rv);
if (sc->sc_ih != NULL) {
pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
sc->sc_ih = NULL;
}
if (sc->sc_ios != 0) {
bus_space_unmap(sc->sc.sc_iot, sc->sc.sc_ioh, sc->sc_ios);
sc->sc_ios = 0;
}
return (rv);
}
int
com_pci_activate(struct device *self, int act)
{
struct com_pci_softc *sc = (struct com_pci_softc *)self;
int i, rv = 0;
if (PCI_VENDOR(sc->sc_id) != PCI_VENDOR_INTEL)
return com_activate(self, act);
switch (act) {
case DVACT_RESUME:
for (i = 0; i < LPSS_REG_NUM; i++)
HWRITE4(sc, i * sizeof(uint32_t), sc->sc_priv[i]);
rv = com_activate(self, act);
break;
case DVACT_SUSPEND:
rv = com_activate(self, act);
for (i = 0; i < LPSS_REG_NUM; i++)
sc->sc_priv[i] = HREAD4(sc, i * sizeof(uint32_t));
break;
default:
rv = com_activate(self, act);
break;
}
return (rv);
}
int
com_pci_intr_designware(void *cookie)
{
struct com_softc *sc = cookie;
com_read_reg(sc, com_usr);
return comintr(sc);
}
|