Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* $NetBSD: adm5120_extio.c,v 1.6 2012/10/27 17:18:01 chs Exp $ */

/*-
 * Copyright (c) 2007 David Young.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */
/*
 * Copyright 2002 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Simon Burge for Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed for the NetBSD Project by
 *      Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: adm5120_extio.c,v 1.6 2012/10/27 17:18:01 chs Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <sys/bus.h>

#include <mips/cache.h>
#include <mips/cpuregs.h>

#include <mips/adm5120/include/adm5120reg.h>
#include <mips/adm5120/include/adm5120var.h>
#include <mips/adm5120/include/adm5120_mainbusvar.h>
#include <mips/adm5120/include/adm5120_extiovar.h>

#include "locators.h"

#ifdef EXTIO_DEBUG
int extio_debug = 1;
#define	EXTIO_DPRINTF(__fmt, ...)		\
do {						\
	if (extio_debug)			\
		printf((__fmt), __VA_ARGS__);	\
} while (/*CONSTCOND*/0)
#else /* !EXTIO_DEBUG */
#define	EXTIO_DPRINTF(__fmt, ...)	do { } while (/*CONSTCOND*/0)
#endif /* EXTIO_DEBUG */

static int	extio_match(device_t, cfdata_t, void *);
static void	extio_attach(device_t, device_t, void *);
static int	extio_submatch(device_t, cfdata_t, const int *, void *);
static int	extio_print(void *, const char *);

CFATTACH_DECL_NEW(extio, sizeof(struct extio_softc),
    extio_match, extio_attach, NULL, NULL);

/* There can be only one. */
int	extio_found;

struct extiodev {
	const char	*ed_name;
	bus_addr_t	ed_addr;
	int		ed_irq;
	uint32_t	ed_gpio_mask;
	int		ed_cfio;
};

struct extiodev extiodevs[] = {
	{"wdc",		ADM5120_BASE_EXTIO0,	0,	__BIT(4),	1},
	{NULL,		0,			0,	0x0,		0},
};

static int
extio_match(device_t parent, cfdata_t match, void *aux)
{
	return !extio_found;
}

static void
extio_attach_args_create(struct extio_attach_args *ea, struct extiodev *ed,
    void *gpio, bus_space_tag_t st)
{
	ea->ea_name = ed->ed_name;
	ea->ea_addr = ed->ed_addr;
	ea->ea_irq = ed->ed_irq;
	ea->ea_st = st;
	ea->ea_gpio = gpio;
	ea->ea_gpio_mask = ed->ed_gpio_mask;
	ea->ea_cfio = ed->ed_cfio;
}

static void
extio_mpmc_dump(struct extio_softc *sc)
{
	EXTIO_DPRINTF("%s: regs:\n"
	    "  ctl 0x%08" PRIx32 "\n"
	    "  sts 0x%08" PRIx32 "\n"
	    "   sc 0x%08" PRIx32 "\n"
	    "  sww 0x%08" PRIx32 "\n"
	    "  swo 0x%08" PRIx32 "\n"
	    "  swr 0x%08" PRIx32 "\n"
	    "  swp 0x%08" PRIx32 "\n"
	    " swwr 0x%08" PRIx32 "\n"
	    "  swt 0x%08" PRIx32 "\n", __func__,
	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL),
	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_STATUS),
	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2)),
	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2)),
	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2)),
	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2)),
	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2)),
	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2)),
	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2)));
}

static void
extio_mpmc_init(struct extio_softc *sc)
{
	int i, s;
#if 0
	uint32_t control;
#endif
	uint32_t status;

	/* Map MultiPort Memory Controller */
	if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
	                  &sc->sc_mpmch) != 0) {
		aprint_error_dev(sc->sc_dev, "unable to map MPMC\n");
		return;
	}

	extio_mpmc_dump(sc);

#if 0
	control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
	    ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_DWB;
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
	    control);
#endif

	s = splhigh();
	/* I wait for MPMC to become idle, and then I enter low-power mode
	 * so that I can safely set the static configuration.
	 */
	for (i = 1000; --i > 0; ) {
		status = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
		    ADM5120_MPMC_STATUS);
		if ((status &
		     (ADM5120_MPMC_STATUS_WBS|ADM5120_MPMC_STATUS_BU)) == 0)
			break;
		delay(10);
	}

	if (i == 0) {
		aprint_error_dev(sc->sc_dev,
		    "timeout waiting for MPMC idle\n");
		splx(s);
		return;
	} else
		EXTIO_DPRINTF("%s: MPMC idle\n", device_xname(sc->sc_dev));

#if 0
	control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
	    ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_ME |
	    ADM5120_MPMC_CONTROL_LPM;
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
	    control);
#endif

	/*
	 * Configure external I/O to suit the CompactFlash card.
	 *
	 * Static Configuration 2
	 *
	 * 1 Enable 'async page mode four'.
	 * 2 'Byte lane state' bits for active low for both read & write.
	 * 3 No buffer, no write protection.
	 * 4 No extended wait.
	 * 5 Active low chip select.
	 * 7 8-bit memory width.
	 */
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2),
	    ADM5120_MPMC_SC_BLS|ADM5120_MPMC_SC_PM|ADM5120_MPMC_SC_MW_8B);

	/*
	 * Static Wait Wen 2: after asserting chip select, wait 3 HCLK cycles
	 * before asserting write enable.
	 */
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2),
	    __SHIFTIN(2, ADM5120_MPMC_SWW_WWE));

	/*
	 * Static Wait Oen 2: after selecting chip select, wait 3 HCLK cycles
	 * before asserting output enable.
	 */
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2),
	    __SHIFTIN(3, ADM5120_MPMC_SWO_WOE));

	/*
	 * Static Wait Rd 2: set wait state time to 27 HCLK cycles.
	 */
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2),
	    __SHIFTIN(26, ADM5120_MPMC_SWR_NMRW));

	/*
	 * Static Wait Wait Page 2: set wait state time to 30 HCLK cycles.
	 */
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2),
	    __SHIFTIN(29, ADM5120_MPMC_SWP_WPS));

	/*
	 * Static Wait Wait Wr 2: set wait state time to 22 HCLK cycles.
	 */
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2),
	    __SHIFTIN(20, ADM5120_MPMC_SWWR_WWS));

	/*
	 * Static Wait Wait Turn 2: 10 HCLK cycles for turnaround.
	 */
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2),
	    __SHIFTIN(9, ADM5120_MPMC_SWT_WAITTURN));

#if 0
	/* Leave low-power mode. */
	control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
	    ADM5120_MPMC_CONTROL) &
	    ~(ADM5120_MPMC_CONTROL_LPM|ADM5120_MPMC_CONTROL_DWB);
	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
	    control);
	splx(s);
#endif

	extio_mpmc_dump(sc);
}

static void
extio_attach(device_t parent, device_t self, void *aux)
{
	struct extio_softc *sc = device_private(self);
	struct mainbus_attach_args *ma = (struct mainbus_attach_args *)aux;
	struct extio_attach_args ea;
	struct extiodev *ed;
	struct adm5120_config *admc = &adm5120_configuration;

	extio_found = 1;
	printf("\n");

	sc->sc_dev = self;
	sc->sc_gpio = ma->ma_gpio;
	sc->sc_obiot = ma->ma_obiot;
	sc->sc_gpioh = ma->ma_gpioh;

	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);

	sc->sc_pm.pm_map = &sc->sc_map[0];

	/* Map GPIO[0] (WAIT#) for input.
	 *
	 * If WAIT# is high (inactive), then enable WAIT# handshake for
	 * EXTIO0 accesses.  Otherwise, assume that WAIT# is
	 * stuck low (active), in which case all accesses would timeout
	 * if we enabled WAIT# handshake.
	 *
	 * Map GPIO[1:2].  Program 5120 to treat GPIO[1:2] as
	 * Chip Select / Interrupt pins for External I/O #0.
	 *
	 * Map GPIO[3:4].  Program 5120 to treat GPIO[3:4] as
	 * Chip Select / Interrupt pins for External I/O #1.
	 *
	 * Use GPIO[4] for interrupts.  (Not yet.)
	 */
	if (gpio_pin_map(sc->sc_gpio, 0, __BITS(0, 4), &sc->sc_pm) != 0) {
		aprint_error_dev(sc->sc_dev, "failed to map GPIO[1:2]\n");
	}
	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 0, GPIO_PIN_INPUT);
	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 1, GPIO_PIN_OUTPUT);
	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 2, GPIO_PIN_INPUT);
	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 3, GPIO_PIN_OUTPUT);
	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 4, GPIO_PIN_INPUT);
	gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 1, 0);
	gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 3, 0);

	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);

	if (gpio_pin_read(sc->sc_gpio, &sc->sc_pm, 0) == GPIO_PIN_HIGH) {
		EXTIO_DPRINTF("%s: WAIT# inactive\n",
		    device_xname(sc->sc_dev));
		bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
		    ADM5120_GPIO2_EW | ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
	} else {
		aprint_error_dev(sc->sc_dev, "WAIT# active; may be stuck\n");
		bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
		    ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
	}
	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);

	/* Map MultiPort Memory Controller */
	if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
	                  &sc->sc_mpmch) != 0) {
		aprint_error_dev(sc->sc_dev, "unable to map MPMC\n");
		return;
	}

	extio_mpmc_init(sc);
	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);

	/* Program 5120 for level interrupts on GPIO[4] (INTX1).  (Not yet.)
	 *
	 * Map interrupt.  (Not yet.  In the mean time, use flags 0x1000 in
	 * kernel configuration so that wdc(4) will expect no interrupts.)
	 */

	cfio_bus_mem_init(&sc->sc_cfio, &admc->extio_space);

	for (ed = extiodevs; ed->ed_name != NULL; ed++) {
		EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
		extio_attach_args_create(&ea, ed, sc->sc_gpio,
		    (ed->ed_cfio) ? &sc->sc_cfio : &admc->extio_space);
		EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
		(void)config_found_sm_loc(self, "extio", NULL, &ea, extio_print,
		    extio_submatch);
	}
	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
	extio_mpmc_dump(sc);
}

static int
extio_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
	struct extio_attach_args *ea = aux;

	if (cf->cf_loc[EXTIOCF_CFIO] != EXTIOCF_CFIO_DEFAULT &&
	    cf->cf_loc[EXTIOCF_CFIO] != ea->ea_cfio)
		return 0;

	if (cf->cf_loc[EXTIOCF_GPIO_MASK] != EXTIOCF_GPIO_MASK_DEFAULT &&
	    cf->cf_loc[EXTIOCF_GPIO_MASK] != ea->ea_gpio_mask)
		return 0;

	if (cf->cf_loc[EXTIOCF_IRQ] != EXTIOCF_IRQ_DEFAULT &&
	    cf->cf_loc[EXTIOCF_IRQ] != ea->ea_irq)
		return 0;

	if (cf->cf_loc[EXTIOCF_ADDR] != EXTIOCF_ADDR_DEFAULT &&
	    cf->cf_loc[EXTIOCF_ADDR] != ea->ea_addr)
		return 0;

	return config_match(parent, cf, aux);
}

static int
extio_print(void *aux, const char *pnp)
{
	struct extio_attach_args *ea = aux;

	if (pnp != NULL)
		aprint_normal("%s at %s", ea->ea_name, pnp);
	if (ea->ea_cfio != EXTIOCF_CFIO_DEFAULT)
		aprint_normal(" cfio");
	if (ea->ea_addr != EXTIOCF_ADDR_DEFAULT)
		aprint_normal(" addr 0x%"PRIxBUSADDR, ea->ea_addr);
	if (ea->ea_gpio_mask != EXTIOCF_GPIO_MASK_DEFAULT)
		aprint_normal(" gpio_mask 0x%02x", ea->ea_gpio_mask);

	return UNCONF;
}