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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* $NetBSD: auspi.c,v 1.8 2012/01/04 02:36:26 kiyohara Exp $ */

/*-
 * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
 * Copyright (c) 2006 Garrett D'Amore.
 * All rights reserved.
 *
 * Portions of this code were written by Garrett D'Amore for the
 * Champaign-Urbana Community Wireless Network Project.
 *
 * 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 acknowledgements:
 *      This product includes software developed by the Urbana-Champaign
 *      Independent Media Center.
 *	This product includes software developed by Garrett D'Amore.
 * 4. Urbana-Champaign Independent Media Center's name and Garrett
 *    D'Amore's name may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
 * MEDIA CENTER AND GARRETT D'AMORE ``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 URBANA-CHAMPAIGN INDEPENDENT
 * MEDIA CENTER OR GARRETT D'AMORE 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: auspi.c,v 1.8 2012/01/04 02:36:26 kiyohara Exp $");

#include "locators.h"

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/systm.h>

#include <mips/alchemy/include/aubusvar.h>
#include <mips/alchemy/include/auvar.h>

#include <mips/alchemy/dev/aupscreg.h>
#include <mips/alchemy/dev/aupscvar.h>
#include <mips/alchemy/dev/auspireg.h>
#include <mips/alchemy/dev/auspivar.h>

#include <dev/spi/spivar.h>

struct auspi_softc {
	device_t		sc_dev;
	struct aupsc_controller	sc_psc;		/* parent controller ops */
	struct spi_controller	sc_spi;		/* SPI implementation ops */
	struct auspi_machdep	sc_md;		/* board-specific support */
	struct auspi_job	*sc_job;	/* current job */
	struct spi_chunk	*sc_wchunk;
	struct spi_chunk	*sc_rchunk;
	void			*sc_ih;		/* interrupt handler */

	struct spi_transfer	*sc_transfer;
	bool			sc_running;	/* is it processing stuff? */

	SIMPLEQ_HEAD(,spi_transfer)	sc_q;
};

#define	auspi_select(sc, slave)	\
	(sc)->sc_md.am_select((sc)->sc_md.am_cookie, (slave))

#define	STATIC

STATIC int auspi_match(device_t, struct cfdata *, void *);
STATIC void auspi_attach(device_t, device_t, void *);
STATIC int auspi_intr(void *);

CFATTACH_DECL_NEW(auspi, sizeof(struct auspi_softc),
    auspi_match, auspi_attach, NULL, NULL);

/* SPI service routines */
STATIC int auspi_configure(void *, int, int, int);
STATIC int auspi_transfer(void *, struct spi_transfer *);

/* internal stuff */
STATIC void auspi_done(struct auspi_softc *, int);
STATIC void auspi_send(struct auspi_softc *);
STATIC void auspi_recv(struct auspi_softc *);
STATIC void auspi_sched(struct auspi_softc *);

#define	GETREG(sc, x)	\
	bus_space_read_4(sc->sc_psc.psc_bust, sc->sc_psc.psc_bush, x)
#define	PUTREG(sc, x, v)	\
	bus_space_write_4(sc->sc_psc.psc_bust, sc->sc_psc.psc_bush, x, v)

int
auspi_match(device_t parent, struct cfdata *cf, void *aux)
{
	struct aupsc_attach_args *aa = aux;

	if (strcmp(aa->aupsc_name, cf->cf_name) != 0)
		return 0;

	return 1;
}

void
auspi_attach(device_t parent, device_t self, void *aux)
{
	struct auspi_softc *sc = device_private(self);
	struct aupsc_attach_args *aa = aux;
	struct spibus_attach_args sba;
	const struct auspi_machdep *md;

	sc->sc_dev = self;

	if ((md = auspi_machdep(aa->aupsc_addr)) != NULL) {
		sc->sc_md = *md;
	}

	aprint_normal(": Alchemy PSC SPI protocol\n");

	sc->sc_psc = aa->aupsc_ctrl;

	/*
	 * Initialize SPI controller
	 */
	sc->sc_spi.sct_cookie = sc;
	sc->sc_spi.sct_configure = auspi_configure;
	sc->sc_spi.sct_transfer = auspi_transfer;

	/* fix this! */
	sc->sc_spi.sct_nslaves = sc->sc_md.am_nslaves;

	sba.sba_controller = &sc->sc_spi;

	/* enable SPI mode */
	sc->sc_psc.psc_enable(sc, AUPSC_SEL_SPI);

	/* initialize the queue */
	SIMPLEQ_INIT(&sc->sc_q);

	/* make sure interrupts disabled at the SPI */
	PUTREG(sc, AUPSC_SPIMSK, SPIMSK_ALL);

	/* enable device interrupts */
	sc->sc_ih = au_intr_establish(aa->aupsc_irq, 0, IPL_BIO, IST_LEVEL,
	    auspi_intr, sc);

	(void) config_found_ia(self, "spibus", &sba, spibus_print);
}

int
auspi_configure(void *arg, int slave, int mode, int speed)
{
	struct auspi_softc *sc = arg;
	int		brg, i;
	uint32_t	reg;

	/* setup interrupt registers */
	PUTREG(sc, AUPSC_SPIMSK, SPIMSK_NORM);

	reg = GETREG(sc, AUPSC_SPICFG);

	reg &= ~(SPICFG_BRG_MASK);	/* clear BRG */
	reg &= ~(SPICFG_DIV_MASK);	/* use pscn_mainclock/2 */
	reg &= ~(SPICFG_PSE);		/* disable port swap */
	reg &= ~(SPICFG_BI);		/* clear bit clock invert */
	reg &= ~(SPICFG_CDE);		/* clear clock phase delay */
	reg &= ~(SPICFG_CGE);		/* clear clock gate enable */
	//reg |= SPICFG_MO;		/* master-only mode */
	reg |= SPICFG_DE;		/* device enable */
	reg |= SPICFG_DD;		/* disable DMA */
	reg |= SPICFG_RT_1;		/* 1 byte rx fifo threshold */
	reg |= SPICFG_TT_1;		/* 1 byte tx fifo threshold */
	reg |= ((8-1) << SPICFG_LEN_SHIFT);/* always work in 8-bit chunks */

	/*
	 * We assume a base clock of 48MHz has been established by the
	 * platform code.  The clock divider reduces this to 24MHz.
	 * Next we have to figure out the BRG
	 */
#define	BASECLK	24000000
	for (brg = 0; brg < 64; brg++) {
		if (speed >= (BASECLK / ((brg + 1) * 2))) {
			break;
		}
	}

	/*
	 * Does the device want to go even slower?  Our minimum speed without
	 * changing other assumptions, and complicating the code even further,
	 * is 24MHz/128, or 187.5kHz.  That should be slow enough for any
	 * device we're likely to encounter.
	 */
	if (speed < (BASECLK / ((brg + 1) * 2))) {
		return EINVAL;
	}
	reg &= ~SPICFG_BRG_MASK;
	reg |= (brg << SPICFG_BRG_SHIFT);

	/*
	 * I'm not entirely confident that these values are correct.
	 * But at least mode 0 appears to work properly with the
	 * devices I have tested.  The documentation seems to suggest
	 * that I have the meaning of the clock delay bit inverted.
	 */
	switch (mode) {
	case SPI_MODE_0:
		reg |= 0;			/* CPHA = 0, CPOL = 0 */
		break;
	case SPI_MODE_1:
		reg |= SPICFG_CDE;		/* CPHA = 1, CPOL = 0 */
		break;
	case SPI_MODE_2:
		reg |= SPICFG_BI;		/* CPHA = 0, CPOL = 1 */
		break;
	case SPI_MODE_3:
		reg |= SPICFG_CDE | SPICFG_BI;	/* CPHA = 1, CPOL = 1 */
		break;
	default:
		return EINVAL;
	}

	PUTREG(sc, AUPSC_SPICFG, reg);

	for (i = 1000000; i; i -= 10) {
		if (GETREG(sc, AUPSC_SPISTAT) & SPISTAT_DR) {
			return 0;
		}
	}

	return ETIMEDOUT;
}

void
auspi_send(struct auspi_softc *sc)
{
	uint32_t		data;
	struct spi_chunk	*chunk;

	/* fill the fifo */
	while ((chunk = sc->sc_wchunk) != NULL) {

		while (chunk->chunk_wresid) {

			/* transmit fifo full? */
			if (GETREG(sc, AUPSC_SPISTAT) & SPISTAT_TF) {
				return;
			}

			if (chunk->chunk_wptr) {
				data = *chunk->chunk_wptr++;
			} else {
				data = 0;
			}
			chunk->chunk_wresid--;

			/* if the last outbound character, mark it */
			if ((chunk->chunk_wresid == 0) &&
			    (chunk->chunk_next == NULL)) {
				data |=  SPITXRX_LC;
			}
			PUTREG(sc, AUPSC_SPITXRX, data);
		}

		/* advance to next transfer */
		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
	}
}

void
auspi_recv(struct auspi_softc *sc)
{
	uint32_t		data;
	struct spi_chunk	*chunk;

	while ((chunk = sc->sc_rchunk) != NULL) {
		while (chunk->chunk_rresid) {

			/* rx fifo empty? */
			if ((GETREG(sc, AUPSC_SPISTAT) & SPISTAT_RE) != 0) {
				return;
			}

			/* collect rx data */
			data = GETREG(sc, AUPSC_SPITXRX);
			if (chunk->chunk_rptr) {
				*chunk->chunk_rptr++ = data & 0xff;
			}

			chunk->chunk_rresid--;
		}

		/* advance next to next transfer */
		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
	}
}

void
auspi_sched(struct auspi_softc *sc)
{
	struct spi_transfer	*st;
	int			err;

	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {

		/* remove the item */
		spi_transq_dequeue(&sc->sc_q);

		/* note that we are working on it */
		sc->sc_transfer = st;

		if ((err = auspi_select(sc, st->st_slave)) != 0) {
			spi_done(st, err);
			continue;
		}

		/* clear the fifos */
		PUTREG(sc, AUPSC_SPIPCR, SPIPCR_RC | SPIPCR_TC);
		/* setup chunks */
		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
		auspi_send(sc);
		/* now kick the master start to get the chip running */
		PUTREG(sc, AUPSC_SPIPCR, SPIPCR_MS);
		sc->sc_running = true;
		return;
	}
	auspi_select(sc, -1);
	sc->sc_running = false;
}

void
auspi_done(struct auspi_softc *sc, int err)
{
	struct spi_transfer	*st;

	/* called from interrupt handler */
	if ((st = sc->sc_transfer) != NULL) {
		sc->sc_transfer = NULL;
		spi_done(st, err);
	}
	/* make sure we clear these bits out */
	sc->sc_wchunk = sc->sc_rchunk = NULL;
	auspi_sched(sc);
}

int
auspi_intr(void *arg)
{
	struct auspi_softc	*sc = arg;
	uint32_t		ev;
	int			err = 0;


	if ((GETREG(sc, AUPSC_SPISTAT) & SPISTAT_DI) == 0) {
		return 0;
	}

	ev = GETREG(sc, AUPSC_SPIEVNT);

	if (ev & SPIMSK_MM) {
		printf("%s: multiple masters detected!\n",
		    device_xname(sc->sc_dev));
		err = EIO;
	}
	if (ev & SPIMSK_RO) {
		printf("%s: receive overflow\n", device_xname(sc->sc_dev));
		err = EIO;
	}
	if (ev & SPIMSK_TU) {
		printf("%s: transmit underflow\n", device_xname(sc->sc_dev));
		err = EIO;
	}
	if (err) {
		/* clear errors */
		PUTREG(sc, AUPSC_SPIEVNT,
		    ev & (SPIMSK_MM | SPIMSK_RO | SPIMSK_TU));
		/* clear the fifos */
		PUTREG(sc, AUPSC_SPIPCR, SPIPCR_RC | SPIPCR_TC);
		auspi_done(sc, err);

	} else {

		/* do all data exchanges */
		auspi_send(sc);
		auspi_recv(sc);

		/*
		 * if the master done bit is set, make sure we do the
		 * right processing.
		 */
		if (ev & SPIMSK_MD) {
			if ((sc->sc_wchunk != NULL) ||
			    (sc->sc_rchunk != NULL)) {
				printf("%s: partial transfer?\n",
				    device_xname(sc->sc_dev));
				err = EIO;
			} 
			auspi_done(sc, err);
		}
		/* clear interrupts */
		PUTREG(sc, AUPSC_SPIEVNT,
		    ev & (SPIMSK_TR | SPIMSK_RR | SPIMSK_MD));
	}

	return 1;
}

int
auspi_transfer(void *arg, struct spi_transfer *st)
{
	struct auspi_softc	*sc = arg;
	int			s;

	/* make sure we select the right chip */
	s = splbio();
	spi_transq_enqueue(&sc->sc_q, st);
	if (sc->sc_running == 0) {
		auspi_sched(sc);
	}
	splx(s);
	return 0;
}