1/*	$NetBSD: pdc.c,v 1.11 2009/10/26 19:16:55 cegger Exp $	*/
2
3/*	$OpenBSD: pdc.c,v 1.10 1999/05/06 02:27:44 mickey Exp $	*/
4
5/*
6 * Copyright (c) 1998-2004 Michael Shalayeff
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/*
31 * Copyright 1996 1995 by Open Software Foundation, Inc.
32 *              All Rights Reserved
33 *
34 * Permission to use, copy, modify, and distribute this software and
35 * its documentation for any purpose and without fee is hereby granted,
36 * provided that the above copyright notice appears in all copies and
37 * that both the copyright notice and this permission notice appear in
38 * supporting documentation.
39 *
40 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
41 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
42 * FOR A PARTICULAR PURPOSE.
43 *
44 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
45 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
46 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
47 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
48 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 *
50 */
51/*
52 * Copyright (c) 1990 mt Xinu, Inc.  All rights reserved.
53 * Copyright (c) 1990 University of Utah.  All rights reserved.
54 *
55 * This file may be freely distributed in any form as long as
56 * this copyright notice is included.
57 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
59 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
60 *
61 *	Utah $Hdr: pdc.c 1.8 92/03/14$
62 */
63
64#include <sys/time.h>
65#include "libsa.h"
66#include <sys/reboot.h>
67#include <sys/disklabel.h>
68
69#include <machine/trap.h>
70#include <machine/pdc.h>
71#include <machine/iomod.h>
72#include <machine/nvm.h>
73#include <machine/param.h>
74#include <machine/cpufunc.h>
75
76#include "dev_hppa.h"
77
78/*
79 * Interface routines to initialize and access the PDC.
80 */
81
82pdcio_t pdc;
83int	pdcbuf[64] PDC_ALIGNMENT;/* PDC return buffer */
84struct	stable_storage sstor;	/* contents of Stable Storage */
85int	sstorsiz;		/* size of Stable Storage */
86struct bootdata bd;
87int bdsize = sizeof(struct bootdata);
88
89/*
90 * Initialize PDC and related variables.
91 */
92void
93pdc_init(void)
94{
95	int err;
96
97	/*
98	 * Initialize important global variables (defined above).
99	 */
100	pdc = (pdcio_t)PAGE0->mem_pdc;
101
102	err = (*pdc)(PDC_STABLE, PDC_STABLE_SIZE, pdcbuf, 0, 0);
103	if (err >= 0) {
104		sstorsiz = min(pdcbuf[0],sizeof(sstor));
105		err = (*pdc)(PDC_STABLE, PDC_STABLE_READ, 0, &sstor, sstorsiz);
106	}
107
108	/*
109	 * Now that we (may) have an output device, if we encountered
110	 * an error reading Stable Storage (above), let them know.
111	 */
112#ifdef DEBUG
113	if (debug && err)
114		printf("Stable storage PDC_STABLE Read Ret'd %d\n", err);
115#endif
116
117	/*
118	 * Clear the FAULT light (so we know when we get a real one)
119	 */
120	(void) (*pdc)(PDC_CHASSIS, PDC_CHASSIS_DISP,
121		      PDC_OSTAT(PDC_OSTAT_BOOT) | 0xCEC0);
122}
123
124/*
125 * Generic READ/WRITE through IODC.  Takes pointer to PDC device
126 * information, returns (positive) number of bytes actually read or
127 * the (negative) error condition, or zero if at "EOF".
128 */
129int
130iodcstrategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
131    size_t *rsize)
132{
133	struct hppa_dev *dp = devdata;
134	struct pz_device *pzdev = dp->pz_dev;
135	int offset, xfer, ret;
136
137#ifdef PDCDEBUG
138	if (debug)
139		printf("iodcstrategy(%p, %s, %u, %u, %p, %p)\n", devdata,
140		       rw==F_READ?"READ":"WRITE", (unsigned) blk, (unsigned) size, buf, rsize);
141
142	if (debug > 1)
143		PZDEV_PRINT(pzdev);
144#endif
145
146	blk <<= DEV_BSHIFT;
147	blk += dp->part_off;
148	if ((pzdev->pz_class & PCL_CLASS_MASK) == PCL_SEQU) {
149		/* rewind and re-read to seek */
150		if (blk < dp->last_blk) {
151#ifdef PDCDEBUG
152			if (debug)
153				printf("iodc: rewind ");
154#endif
155			twiddle();
156			if ((ret = (pzdev->pz_iodc_io)(pzdev->pz_hpa,
157				IODC_IO_READ, pzdev->pz_spa, pzdev->pz_layers,
158				pdcbuf, 0, dp->buf, 0, 0)) < 0) {
159#ifdef DEBUG
160				if (debug)
161					printf("IODC_IO: %d\n", ret);
162#endif
163				return (EIO);
164			} else {
165				dp->last_blk = 0;
166				dp->last_read = 0;
167			}
168		}
169
170#ifdef PDCDEBUG
171		if (debug)
172			printf("seek %d ", (int) dp->last_blk);
173#endif
174		for (; (dp->last_blk + dp->last_read) <= blk;
175		     dp->last_read = ret) {
176			twiddle();
177			dp->last_blk += dp->last_read;
178			if ((ret = (pzdev->pz_iodc_io)(pzdev->pz_hpa,
179				IODC_IO_READ, pzdev->pz_spa, pzdev->pz_layers,
180				pdcbuf, (unsigned) dp->last_blk, dp->buf, IODC_MAXIOSIZ,
181				IODC_MAXIOSIZ)) < 0) {
182#ifdef DEBUG
183				if (debug)
184					printf("IODC_IO: %d\n", ret);
185#endif
186				return (EIO);
187			}
188			if ((ret = pdcbuf[0]) == 0)
189				break;
190#ifdef PDCDEBUG
191			if (debug)
192				printf("-");
193#endif
194		}
195#ifdef PDCDEBUG
196		if (debug)
197			printf("> %d[%d]\n", (int)dp->last_blk, (int)dp->last_read);
198#endif
199	}
200
201	xfer = 0;
202	/* see if we can scratch anything from buffer */
203	if (dp->last_blk <= blk && (dp->last_blk + dp->last_read) > blk) {
204		twiddle();
205		offset = blk - dp->last_blk;
206		xfer = min(dp->last_read - offset, size);
207		size -= xfer;
208		blk += xfer;
209#ifdef PDCDEBUG
210		if (debug)
211			printf("off=%d,xfer=%d,size=%d,blk=%d\n",
212			       offset, xfer, (int)size, (int)blk);
213#endif
214		memcpy(buf, dp->buf + offset, xfer);
215		buf = (char *) buf + xfer;
216	}
217
218	/*
219	 * double buffer it all the time, to cache
220	 */
221	for (; size;
222	    size -= ret, buf = (char *) buf + ret, blk += ret, xfer += ret) {
223		twiddle();
224		offset = blk & IOPGOFSET;
225		if ((ret = (pzdev->pz_iodc_io)(pzdev->pz_hpa,
226				(rw == F_READ? IODC_IO_READ: IODC_IO_WRITE),
227				pzdev->pz_spa, pzdev->pz_layers, pdcbuf,
228				(int)blk - offset, dp->buf, IODC_MAXIOSIZ,
229				IODC_MAXIOSIZ)) < 0) {
230#ifdef DEBUG
231			if (debug)
232				printf("iodc_read(%d,%d): %d\n",
233					(int)blk - offset, IODC_MAXIOSIZ, ret);
234#endif
235			if (xfer)
236				break;
237			else
238				return (EIO);
239		}
240		if ((ret = pdcbuf[0]) <= 0)
241			break;
242		dp->last_blk = blk - offset;
243		dp->last_read = ret;
244		if ((ret -= offset) > size)
245			ret = size;
246		memcpy(buf, dp->buf + offset, ret);
247#ifdef PDCDEBUG
248		if (debug)
249			printf("read %d(%d,%d)@%x ", ret,
250			       (int)dp->last_blk, (int)dp->last_read, (u_int)buf);
251#endif
252	    }
253
254#ifdef PDCDEBUG
255	if (debug)
256		printf("\n");
257#endif
258
259	if (rsize)
260		*rsize = xfer;
261	return (0);
262}
263
264/*
265 * Find a device with specified unit number
266 * (any if unit == -1), and of specified class (PCL_*).
267 */
268struct pz_device *
269pdc_findev(int unit, int class)
270{
271	static struct pz_device pz;
272	int layers[sizeof(pz.pz_layers)/sizeof(pz.pz_layers[0])];
273	iodcio_t iodc;
274	struct iomod *io;
275	int err = 0;
276
277#ifdef	PDCDEBUG
278	if (debug)
279		printf("pdc_finddev(%d, %x)\n", unit, class);
280#endif
281	iodc = (iodcio_t)(PAGE0->mem_free + IODC_MAXSIZE);
282	io = PAGE0->mem_boot.pz_hpa;
283
284	/* quick hack for boot device */
285	if (PAGE0->mem_boot.pz_class == class &&
286	    (unit == -1 || PAGE0->mem_boot.pz_layers[0] == unit)) {
287
288		bcopy (&PAGE0->mem_boot.pz_dp, &pz.pz_dp, sizeof(pz.pz_dp));
289		bcopy (pz.pz_layers, layers, sizeof(layers));
290		if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, pdcbuf, io,
291				  IODC_INIT, iodc, IODC_MAXSIZE)) < 0) {
292#ifdef DEBUG
293			if (debug)
294				printf("IODC_READ: %d\n", err);
295#endif
296			return NULL;
297		}
298	} else {
299		struct pdc_memmap memmap;
300		struct iodc_data mptr;
301		int i, stp;
302
303		for (i = 0; i < 0xf; i++) {
304			pz.pz_bc[0] = pz.pz_bc[1] =
305			pz.pz_bc[2] = pz.pz_bc[3] = -1;
306			pz.pz_bc[4] = 2;
307			pz.pz_bc[5] = 0;	/* core bus */
308			pz.pz_mod = i;
309			if ((pdc)(PDC_MEMMAP, PDC_MEMMAP_HPA, &memmap,
310				  &pz.pz_dp) < 0)
311				continue;
312#ifdef PDCDEBUG
313			if (debug)
314				printf("memap: %d.%d.%d, hpa=%x, mpgs=%x\n",
315				       pz.pz_bc[4], pz.pz_bc[5], pz.pz_mod,
316				       memmap.hpa, memmap.morepages);
317#endif
318			io = (struct iomod *) memmap.hpa;
319
320			if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, &pdcbuf, io,
321				   IODC_DATA, &mptr, sizeof(mptr))) < 0) {
322#ifdef DEBUG
323				if (debug)
324					printf("IODC_DATA: %d\n", err);
325#endif
326				continue;
327			}
328
329			if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, pdcbuf, io,
330					  IODC_INIT, iodc, IODC_MAXSIZE)) < 0) {
331#ifdef DEBUG
332				if (debug)
333					printf("IODC_READ: %d\n", err);
334#endif
335				continue;
336			}
337
338			stp = IODC_INIT_FIRST;
339			do {
340				if ((err = (iodc)(io, stp, io->io_spa, layers,
341						  pdcbuf, 0, 0, 0, 0)) < 0) {
342#ifdef DEBUG
343					if (debug && err != PDC_ERR_EOD)
344						printf("IODC_INIT_%s: %d\n",
345						       stp==IODC_INIT_FIRST?
346						       "FIRST":"NEXT", err);
347#endif
348					break;
349				}
350#ifdef PDCDEBUG
351				if (debug)
352					printf("[%x,%x,%x,%x,%x,%x], "
353					       "[%x,%x,%x,%x,%x,%x]\n",
354					       pdcbuf[0], pdcbuf[1], pdcbuf[2],
355					       pdcbuf[3], pdcbuf[4], pdcbuf[5],
356					       layers[0], layers[1], layers[2],
357					       layers[3], layers[4], layers[5]);
358#endif
359				stp = IODC_INIT_NEXT;
360
361			} while (pdcbuf[1] != class &&
362				 unit != -1 && unit != layers[0]);
363
364			if (err >= 0)
365				break;
366		}
367	}
368
369	if (err >= 0) {
370		/* init device */
371		if (0  && (err = (iodc)(io, IODC_INIT_DEV, io->io_spa,
372				  layers, pdcbuf, 0, 0, 0, 0)) < 0) {
373#ifdef DEBUG
374			if (debug)
375				printf("INIT_DEV: %d\n", err);
376#endif
377			return NULL;
378		}
379
380		/* read i/o entry code */
381		if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, pdcbuf, io,
382				IODC_IO, iodc, IODC_MAXSIZE)) < 0) {
383#ifdef DEBUG
384			if (debug)
385				printf("IODC_READ: %d\n", err);
386#endif
387			return NULL;
388		}
389
390		pz.pz_flags = 0;
391		memcpy(pz.pz_layers, layers, sizeof(pz.pz_layers));
392		pz.pz_hpa = io;
393/* XXX		pz.pz_spa = io->io_spa; */
394		pz.pz_iodc_io = iodc;
395		pz.pz_class = class;
396
397		return &pz;
398	}
399
400	return NULL;
401}
402
403static inline void
404fall(int c_base, int c_count, int c_loop, int c_stride, int data)
405{
406	int loop;
407
408	for (; c_count--; c_base += c_stride)
409		for (loop = c_loop; loop--; )
410			if (data)
411				fdce(0, c_base);
412			else
413				fice(0, c_base);
414
415}
416
417/*
418 * fcacheall - Flush all caches.
419 *
420 * This routine is just a wrapper around the real cache flush routine.
421 */
422struct pdc_cache pdc_cacheinfo PDC_ALIGNMENT;
423
424void
425fcacheall(void)
426{
427	int err;
428
429	if ((err = (*pdc)(PDC_CACHE, PDC_CACHE_DFLT, &pdc_cacheinfo)) < 0) {
430#ifdef DEBUG
431		if (debug)
432			printf("fcacheall: PDC_CACHE failed (%d).\n", err);
433#endif
434		return;
435	}
436#if PDCDEBUG
437	if (debug)
438		printf("pdc_cache:\nic={%u,%x,%x,%u,%u,%u}\n"
439		       "dc={%u,%x,%x,%u,%u,%u}\n",
440		       pdc_cacheinfo.ic_size, *(u_int*)&pdc_cacheinfo.ic_conf,
441		       pdc_cacheinfo.ic_base, pdc_cacheinfo.ic_stride,
442		       pdc_cacheinfo.ic_count, pdc_cacheinfo.ic_loop,
443		       pdc_cacheinfo.dc_size, *(u_int*)&pdc_cacheinfo.ic_conf,
444		       pdc_cacheinfo.dc_base, pdc_cacheinfo.dc_stride,
445		       pdc_cacheinfo.dc_count, pdc_cacheinfo.dc_loop);
446#endif
447	/*
448	 * Flush the instruction, then data cache.
449	 */
450	fall(pdc_cacheinfo.ic_base, pdc_cacheinfo.ic_count,
451	     pdc_cacheinfo.ic_loop, pdc_cacheinfo.ic_stride, 0);
452	sync_caches();
453	fall(pdc_cacheinfo.dc_base, pdc_cacheinfo.dc_count,
454	     pdc_cacheinfo.dc_loop, pdc_cacheinfo.dc_stride, 1);
455	sync_caches();
456}
457