octeon_ebt3000_cf.c revision 226020
1203130Simp/***********************license start***************
2203130Simp *  Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights
3203130Simp *  reserved.
4203130Simp *
5203130Simp *
6203130Simp *  Redistribution and use in source and binary forms, with or without
7203130Simp *  modification, are permitted provided that the following conditions are
8203130Simp *  met:
9203130Simp *
10203130Simp *      * Redistributions of source code must retain the above copyright
11203130Simp *        notice, this list of conditions and the following disclaimer.
12203130Simp *
13203130Simp *      * Redistributions in binary form must reproduce the above
14203130Simp *        copyright notice, this list of conditions and the following
15203130Simp *        disclaimer in the documentation and/or other materials provided
16203130Simp *        with the distribution.
17203130Simp *
18203130Simp *      * Neither the name of Cavium Networks nor the names of
19203130Simp *        its contributors may be used to endorse or promote products
20203130Simp *        derived from this software without specific prior written
21203130Simp *        permission.
22203130Simp *
23203130Simp *  TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24203130Simp *  AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25203130Simp *  OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26203130Simp *  RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27203130Simp *  REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28203130Simp *  DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29203130Simp *  OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30203130Simp *  PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31203130Simp *  POSSESSION OR CORRESPONDENCE TO DESCRIPTION.  THE ENTIRE RISK ARISING OUT
32203130Simp *  OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
33203130Simp *
34203130Simp *
35203130Simp *  For any questions regarding licensing please contact marketing@caviumnetworks.com
36203130Simp *
37203130Simp ***********************license end**************************************/
38203130Simp
39194149Simp/*
40194149Simp *  octeon_ebt3000_cf.c
41194149Simp *
42194149Simp */
43194149Simp
44194149Simp#include <sys/cdefs.h>
45194149Simp__FBSDID("$FreeBSD: head/sys/mips/cavium/octeon_ebt3000_cf.c 226020 2011-10-04 18:03:55Z marcel $");
46194149Simp
47194149Simp#include <sys/param.h>
48194149Simp#include <sys/bio.h>
49194149Simp#include <sys/systm.h>
50194149Simp#include <sys/sysctl.h>
51210311Sjmallett#include <sys/ata.h>
52194149Simp#include <sys/bus.h>
53194149Simp#include <sys/kernel.h>
54194149Simp#include <sys/module.h>
55194149Simp#include <sys/rman.h>
56194149Simp#include <sys/power.h>
57194149Simp#include <sys/smp.h>
58194149Simp#include <sys/time.h>
59194149Simp#include <sys/timetc.h>
60194149Simp#include <sys/malloc.h>
61194149Simp
62194149Simp#include <geom/geom.h>
63194149Simp
64194149Simp#include <machine/clock.h>
65194149Simp#include <machine/locore.h>
66194149Simp#include <machine/md_var.h>
67194149Simp#include <machine/cpuregs.h>
68194149Simp
69202063Simp#include <mips/cavium/octeon_pcmap_regs.h>
70194149Simp
71210311Sjmallett#include <contrib/octeon-sdk/cvmx.h>
72210311Sjmallett
73194149Simp/* ATA Commands */
74194149Simp#define CMD_READ_SECTOR		0x20
75194149Simp#define CMD_WRITE_SECTOR	0x30
76194149Simp#define CMD_IDENTIFY		0xEC
77194149Simp
78194149Simp/* The ATA Task File */
79194149Simp#define TF_DATA			0x00
80194149Simp#define TF_ERROR		0x01
81194149Simp#define TF_PRECOMP		0x01
82194149Simp#define TF_SECTOR_COUNT		0x02
83194149Simp#define TF_SECTOR_NUMBER	0x03
84194149Simp#define TF_CYL_LSB		0x04
85194149Simp#define TF_CYL_MSB		0x05
86194149Simp#define TF_DRV_HEAD		0x06
87194149Simp#define TF_STATUS		0x07
88194149Simp#define TF_COMMAND		0x07
89194149Simp
90194149Simp/* Status Register */
91194149Simp#define STATUS_BSY		0x80	/* Drive is busy */
92194149Simp#define STATUS_RDY		0x40	/* Drive is ready */
93204789Sjmallett#define STATUS_DF		0x20	/* Device fault */
94194149Simp#define STATUS_DRQ		0x08	/* Data can be transferred */
95194149Simp
96194149Simp/* Miscelaneous */
97194149Simp#define SECTOR_SIZE		512
98194149Simp#define WAIT_DELAY		1000
99194149Simp#define NR_TRIES		1000
100194149Simp#define SWAP_SHORT(x)		((x << 8) | (x >> 8))
101194149Simp#define MODEL_STR_SIZE		40
102194149Simp
103210311Sjmallett/* XXX */
104210311Sjmallettextern cvmx_bootinfo_t *octeon_bootinfo;
105194149Simp
106194149Simp/* Globals */
107222671Simp/*
108222671Simp * There's three bus types supported by this driver.
109222671Simp *
110222671Simp * CF_8 -- Traditional PC Card IDE interface on an 8-bit wide bus.  We assume
111222671Simp * the bool loader has configure attribute memory properly.  We then access
112222671Simp * the device like old-school 8-bit IDE card (which is all a traditional PC Card
113222671Simp * interface really is).
114222671Simp * CF_16 -- Traditional PC Card IDE interface on a 16-bit wide bus.  Registers on
115222671Simp * this bus are 16-bits wide too.  When accessing registers in the task file, you
116222671Simp * have to do it in 16-bit chunks, and worry about masking out what you don't want
117222671Simp * or ORing together the traditional 8-bit values.  We assume the bootloader does
118222671Simp * the right attribute memory initialization dance.
119222671Simp * CF_TRUE_IDE_8 - CF Card wired to True IDE mode.  There's no Attribute memory
120222671Simp * space at all.  Instead all the traditional 8-bit registers are there, but
121222671Simp * on a 16-bit bus where addr0 isn't wired.  This means we need to read/write them
122222671Simp * 16-bit chunks, but only the lower 8 bits are valid.  We do not (and can not)
123222671Simp * access this like CF_16 with the comingled registers.  Yet we can't access
124222671Simp * this like CF_8 because of the register offset.  Except the TF_DATA register
125222671Simp * appears to be full width?
126222671Simp */
127194149Simpvoid	*base_addr;
128222671Simpint	bus_type;
129222671Simp#define CF_8		1	/* 8-bit bus, no offsets - PC Card */
130222671Simp#define CF_16		2	/* 16-bit bus, registers shared - PC Card */
131222671Simp#define CF_TRUE_IDE_8	3	/* 16-bit bus, only lower 8-bits, TrueIDE */
132222671Simpconst char *const cf_type[] = {
133222671Simp	"impossible type",
134222671Simp	"CF 8-bit",
135222671Simp	"CF 16-bit",
136222671Simp	"True IDE"
137222671Simp};
138194149Simp
139194149Simp/* Device softc */
140194149Simpstruct cf_priv {
141194149Simp	device_t dev;
142194149Simp	struct drive_param *drive_param;
143194149Simp
144194149Simp	struct bio_queue_head cf_bq;
145194149Simp	struct g_geom *cf_geom;
146194149Simp	struct g_provider *cf_provider;
147194149Simp
148194149Simp};
149194149Simp
150194149Simp/* Device parameters */
151194149Simpstruct drive_param{
152194149Simp	union {
153194149Simp		char buf[SECTOR_SIZE];
154210311Sjmallett		struct ata_params driveid;
155194149Simp	} u;
156194149Simp
157194149Simp	char model[MODEL_STR_SIZE];
158194149Simp	uint32_t nr_sectors;
159194149Simp	uint16_t sector_size;
160194149Simp	uint16_t heads;
161194149Simp	uint16_t tracks;
162194149Simp	uint16_t sec_track;
163194149Simp
164194149Simp} drive_param;
165194149Simp
166194149Simp/* GEOM class implementation */
167194149Simpstatic g_access_t       cf_access;
168194149Simpstatic g_start_t        cf_start;
169194149Simpstatic g_ioctl_t        cf_ioctl;
170194149Simp
171194149Simpstruct g_class g_cf_class = {
172194149Simp        .name =         "CF",
173194149Simp        .version =      G_VERSION,
174194149Simp        .start =        cf_start,
175194149Simp        .access =       cf_access,
176194149Simp        .ioctl =        cf_ioctl,
177194149Simp};
178194149Simp
179214708SjmallettDECLARE_GEOM_CLASS(g_cf_class, g_cf);
180214708Sjmallett
181194149Simp/* Device methods */
182194149Simpstatic int	cf_probe(device_t);
183194149Simpstatic void	cf_identify(driver_t *, device_t);
184194149Simpstatic int	cf_attach(device_t);
185194149Simpstatic int	cf_attach_geom(void *, int);
186194149Simp
187194149Simp/* ATA methods */
188204789Sjmallettstatic int	cf_cmd_identify(void);
189204789Sjmallettstatic int	cf_cmd_write(uint32_t, uint32_t, void *);
190204789Sjmallettstatic int	cf_cmd_read(uint32_t, uint32_t, void *);
191204789Sjmallettstatic int	cf_wait_busy(void);
192204789Sjmallettstatic int	cf_send_cmd(uint32_t, uint8_t);
193194149Simpstatic void	cf_attach_geom_proxy(void *arg, int flag);
194194149Simp
195194149Simp/* Miscelenous */
196194149Simpstatic void	cf_swap_ascii(unsigned char[], char[]);
197194149Simp
198194149Simp
199194149Simp/* ------------------------------------------------------------------- *
200194149Simp *                      cf_access()                                    *
201194149Simp * ------------------------------------------------------------------- */
202194149Simpstatic int cf_access (struct g_provider *pp, int r, int w, int e)
203194149Simp{
204194149Simp
205194149Simp	pp->sectorsize = drive_param.sector_size;
206194149Simp        pp->stripesize = drive_param.heads * drive_param.sec_track * drive_param.sector_size;
207194149Simp        pp->mediasize  = pp->stripesize * drive_param.tracks;
208194149Simp
209194149Simp	return (0);
210194149Simp}
211194149Simp
212194149Simp
213194149Simp/* ------------------------------------------------------------------- *
214194149Simp *                      cf_start()                                     *
215194149Simp * ------------------------------------------------------------------- */
216194149Simpstatic void cf_start (struct bio *bp)
217194149Simp{
218204789Sjmallett	int error;
219204789Sjmallett
220194149Simp	/*
221194149Simp	* Handle actual I/O requests. The request is passed down through
222194149Simp	* the bio struct.
223194149Simp	*/
224194149Simp
225194149Simp	if(bp->bio_cmd & BIO_GETATTR) {
226194149Simp		if (g_handleattr_int(bp, "GEOM::fwsectors", drive_param.sec_track))
227194149Simp                        return;
228194149Simp                if (g_handleattr_int(bp, "GEOM::fwheads",   drive_param.heads))
229194149Simp                        return;
230194149Simp                g_io_deliver(bp, ENOIOCTL);
231194149Simp                return;
232194149Simp	}
233194149Simp
234194149Simp	if ((bp->bio_cmd & (BIO_READ | BIO_WRITE))) {
235194149Simp
236194149Simp		if (bp->bio_cmd & BIO_READ) {
237204789Sjmallett			error = cf_cmd_read(bp->bio_length / drive_param.sector_size,
238204789Sjmallett			    bp->bio_offset / drive_param.sector_size, bp->bio_data);
239194149Simp		} else if (bp->bio_cmd & BIO_WRITE) {
240204789Sjmallett			error = cf_cmd_write(bp->bio_length / drive_param.sector_size,
241204789Sjmallett			    bp->bio_offset/drive_param.sector_size, bp->bio_data);
242204789Sjmallett		} else {
243204789Sjmallett			printf("%s: unrecognized bio_cmd %x.\n", __func__, bp->bio_cmd);
244204789Sjmallett			error = ENOTSUP;
245194149Simp		}
246194149Simp
247204789Sjmallett		if (error != 0) {
248204789Sjmallett			g_io_deliver(bp, error);
249204789Sjmallett			return;
250204789Sjmallett		}
251204789Sjmallett
252194149Simp		bp->bio_resid = 0;
253194149Simp		bp->bio_completed = bp->bio_length;
254194149Simp		g_io_deliver(bp, 0);
255194149Simp	}
256194149Simp}
257194149Simp
258194149Simp
259194149Simpstatic int cf_ioctl (struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
260194149Simp{
261222671Simp	return (0);
262194149Simp}
263194149Simp
264194149Simp
265222671Simpstatic uint8_t cf_inb_8(int port)
266222671Simp{
267222671Simp	/*
268222671Simp	 * Traditional 8-bit PC Card/CF bus access.
269222671Simp	 */
270222671Simp	if (bus_type == CF_8) {
271222671Simp		volatile uint8_t *task_file = (volatile uint8_t *)base_addr;
272222671Simp		return task_file[port];
273222671Simp	}
274222671Simp
275222671Simp	/*
276222671Simp	 * True IDE access.  lower 8 bits on a 16-bit bus (see above).
277222671Simp	 */
278222671Simp	volatile uint16_t *task_file = (volatile uint16_t *)base_addr;
279222671Simp	return task_file[port] & 0xff;
280222671Simp}
281222671Simp
282222671Simpstatic void cf_outb_8(int port, uint8_t val)
283222671Simp{
284222671Simp	/*
285222671Simp	 * Traditional 8-bit PC Card/CF bus access.
286222671Simp	 */
287222671Simp	if (bus_type == CF_8) {
288222671Simp		volatile uint8_t *task_file = (volatile uint8_t *)base_addr;
289222671Simp		task_file[port] = val;
290222671Simp	}
291222671Simp
292222671Simp	/*
293222671Simp	 * True IDE access.  lower 8 bits on a 16-bit bus (see above).
294222671Simp	 */
295222671Simp	volatile uint16_t *task_file = (volatile uint16_t *)base_addr;
296222671Simp	task_file[port] = val & 0xff;
297222671Simp}
298222671Simp
299222671Simpstatic uint8_t cf_inb_16(int port)
300222671Simp{
301222671Simp	volatile uint16_t *task_file = (volatile uint16_t *)base_addr;
302222671Simp	uint16_t val = task_file[port / 2];
303222671Simp	if (port & 1)
304222671Simp		return (val >> 8) & 0xff;
305222671Simp	return val & 0xff;
306222671Simp}
307222671Simp
308222671Simpstatic uint16_t cf_inw_16(int port)
309222671Simp{
310222671Simp	volatile uint16_t *task_file = (volatile uint16_t *)base_addr;
311222671Simp	uint16_t val = task_file[port / 2];
312222671Simp	return val;
313222671Simp}
314222671Simp
315222671Simpstatic void cf_outw_16(int port, uint16_t val)
316222671Simp{
317222671Simp	volatile uint16_t *task_file = (volatile uint16_t *)base_addr;
318222671Simp	task_file[port / 2] = val;
319222671Simp}
320222671Simp
321194149Simp/* ------------------------------------------------------------------- *
322194149Simp *                      cf_cmd_read()                                  *
323194149Simp * ------------------------------------------------------------------- *
324194149Simp *
325194149Simp *  Read nr_sectors from the device starting from start_sector.
326194149Simp */
327204789Sjmallettstatic int cf_cmd_read (uint32_t nr_sectors, uint32_t start_sector, void *buf)
328194149Simp{
329194149Simp	unsigned long lba;
330194149Simp	uint32_t count;
331194149Simp	uint16_t *ptr_16;
332194149Simp	uint8_t  *ptr_8;
333204789Sjmallett	int error;
334194149Simp
335194149Simp//#define OCTEON_VISUAL_CF_0 1
336194149Simp#ifdef OCTEON_VISUAL_CF_0
337194149Simp        octeon_led_write_char(0, 'R');
338194149Simp#endif
339194149Simp	ptr_8  = (uint8_t*)buf;
340194149Simp	ptr_16 = (uint16_t*)buf;
341194149Simp	lba = start_sector;
342194149Simp
343194149Simp
344194149Simp	while (nr_sectors--) {
345204789Sjmallett		error = cf_send_cmd(lba, CMD_READ_SECTOR);
346204789Sjmallett		if (error != 0) {
347204789Sjmallett			printf("%s: cf_send_cmd(CMD_READ_SECTOR) failed: %d\n", __func__, error);
348204789Sjmallett			return (error);
349204789Sjmallett		}
350194149Simp
351222671Simp		switch (bus_type)
352222671Simp		{
353222671Simp		case CF_8:
354194149Simp			for (count = 0; count < SECTOR_SIZE; count++) {
355222671Simp				*ptr_8++ = cf_inb_8(TF_DATA);
356222671Simp				if ((count & 0xf) == 0)
357222671Simp					(void)cf_inb_8(TF_STATUS);
358194149Simp			}
359222671Simp			break;
360222671Simp		case CF_TRUE_IDE_8:
361222671Simp		case CF_16:
362222671Simp		default:
363194149Simp			for (count = 0; count < SECTOR_SIZE; count+=2) {
364194149Simp				uint16_t temp;
365222671Simp				temp = cf_inw_16(TF_DATA);
366194149Simp				*ptr_16++ = SWAP_SHORT(temp);
367222671Simp				if ((count & 0xf) == 0)
368222671Simp					(void)cf_inb_16(TF_STATUS);
369194149Simp			}
370222671Simp			break;
371194149Simp		}
372194149Simp
373222671Simp		lba++;
374194149Simp	}
375194149Simp#ifdef OCTEON_VISUAL_CF_0
376194149Simp        octeon_led_write_char(0, ' ');
377194149Simp#endif
378204789Sjmallett	return (0);
379194149Simp}
380194149Simp
381194149Simp
382194149Simp/* ------------------------------------------------------------------- *
383194149Simp *                      cf_cmd_write()                                 *
384194149Simp * ------------------------------------------------------------------- *
385194149Simp *
386194149Simp * Write nr_sectors to the device starting from start_sector.
387194149Simp */
388204789Sjmallettstatic int cf_cmd_write (uint32_t nr_sectors, uint32_t start_sector, void *buf)
389194149Simp{
390194149Simp	uint32_t lba;
391194149Simp	uint32_t count;
392194149Simp	uint16_t *ptr_16;
393194149Simp	uint8_t  *ptr_8;
394204789Sjmallett	int error;
395194149Simp
396194149Simp//#define OCTEON_VISUAL_CF_1 1
397194149Simp#ifdef OCTEON_VISUAL_CF_1
398194149Simp        octeon_led_write_char(1, 'W');
399194149Simp#endif
400194149Simp	lba = start_sector;
401194149Simp	ptr_8  = (uint8_t*)buf;
402194149Simp	ptr_16 = (uint16_t*)buf;
403194149Simp
404194149Simp	while (nr_sectors--) {
405204789Sjmallett		error = cf_send_cmd(lba, CMD_WRITE_SECTOR);
406204789Sjmallett		if (error != 0) {
407204789Sjmallett			printf("%s: cf_send_cmd(CMD_WRITE_SECTOR) failed: %d\n", __func__, error);
408204789Sjmallett			return (error);
409204789Sjmallett		}
410194149Simp
411222671Simp		switch (bus_type)
412222671Simp		{
413222671Simp		case CF_8:
414194149Simp			for (count = 0; count < SECTOR_SIZE; count++) {
415222671Simp				cf_outb_8(TF_DATA, *ptr_8++);
416222671Simp				if ((count & 0xf) == 0)
417222671Simp					(void)cf_inb_8(TF_STATUS);
418194149Simp			}
419222671Simp			break;
420222671Simp		case CF_TRUE_IDE_8:
421222671Simp		case CF_16:
422222671Simp		default:
423194149Simp			for (count = 0; count < SECTOR_SIZE; count+=2) {
424194149Simp				uint16_t temp = *ptr_16++;
425222671Simp				cf_outw_16(TF_DATA, SWAP_SHORT(temp));
426222671Simp				if ((count & 0xf) == 0)
427222671Simp					(void)cf_inb_16(TF_STATUS);
428194149Simp			}
429222671Simp			break;
430194149Simp		}
431194149Simp
432222671Simp		lba++;
433194149Simp	}
434194149Simp#ifdef OCTEON_VISUAL_CF_1
435194149Simp        octeon_led_write_char(1, ' ');
436194149Simp#endif
437204789Sjmallett	return (0);
438194149Simp}
439194149Simp
440194149Simp
441194149Simp/* ------------------------------------------------------------------- *
442194149Simp *                      cf_cmd_identify()                              *
443194149Simp * ------------------------------------------------------------------- *
444194149Simp *
445194149Simp * Read parameters and other information from the drive and store
446194149Simp * it in the drive_param structure
447194149Simp *
448194149Simp */
449204789Sjmallettstatic int cf_cmd_identify (void)
450194149Simp{
451194149Simp	int count;
452204789Sjmallett	int error;
453194149Simp
454222671Simp	error = cf_send_cmd(0, CMD_IDENTIFY);
455204789Sjmallett	if (error != 0) {
456204789Sjmallett		printf("%s: identify failed: %d\n", __func__, error);
457204789Sjmallett		return (error);
458204789Sjmallett	}
459222671Simp	switch (bus_type)
460222671Simp	{
461222671Simp	case CF_8:
462222671Simp		for (count = 0; count < SECTOR_SIZE; count++)
463222671Simp			drive_param.u.buf[count] = cf_inb_8(TF_DATA);
464222671Simp		break;
465222671Simp	case CF_TRUE_IDE_8:
466222671Simp	case CF_16:
467222671Simp	default:
468222671Simp		for (count = 0; count < SECTOR_SIZE; count += 2) {
469222671Simp			uint16_t temp;
470222671Simp			temp = cf_inw_16(TF_DATA);
471222671Simp
472222671Simp			/* endianess will be swapped below */
473222671Simp			drive_param.u.buf[count]   = (temp & 0xff);
474222671Simp			drive_param.u.buf[count + 1] = (temp & 0xff00) >> 8;
475222671Simp		}
476222671Simp		break;
477222671Simp	}
478194149Simp
479194149Simp	cf_swap_ascii(drive_param.u.driveid.model, drive_param.model);
480194149Simp
481194149Simp	drive_param.sector_size =  512;   //=  SWAP_SHORT (drive_param.u.driveid.sector_bytes);
482210311Sjmallett	drive_param.heads 	=  SWAP_SHORT (drive_param.u.driveid.current_heads);
483210311Sjmallett	drive_param.tracks	=  SWAP_SHORT (drive_param.u.driveid.current_cylinders);
484210311Sjmallett	drive_param.sec_track   =  SWAP_SHORT (drive_param.u.driveid.current_sectors);
485210311Sjmallett	drive_param.nr_sectors  = (uint32_t)SWAP_SHORT (drive_param.u.driveid.lba_size_1) |
486210311Sjmallett	    ((uint32_t)SWAP_SHORT (drive_param.u.driveid.lba_size_2));
487226020Smarcel	if (bootverbose) {
488226020Smarcel		printf("    model %s\n", drive_param.model);
489226020Smarcel		printf("    heads %d tracks %d sec_tracks %d sectors %d\n",
490226020Smarcel			drive_param.heads, drive_param.tracks,
491226020Smarcel			drive_param.sec_track, drive_param.nr_sectors);
492226020Smarcel	}
493194149Simp
494204789Sjmallett	return (0);
495194149Simp}
496194149Simp
497194149Simp
498194149Simp/* ------------------------------------------------------------------- *
499194149Simp *                      cf_send_cmd()                                  *
500194149Simp * ------------------------------------------------------------------- *
501194149Simp *
502194149Simp * Send command to read/write one sector specified by lba.
503194149Simp *
504194149Simp */
505204789Sjmallettstatic int cf_send_cmd (uint32_t lba, uint8_t cmd)
506194149Simp{
507222671Simp	switch (bus_type)
508222671Simp	{
509222671Simp	case CF_8:
510222671Simp	case CF_TRUE_IDE_8:
511222671Simp		while (cf_inb_8(TF_STATUS) & STATUS_BSY)
512194149Simp			DELAY(WAIT_DELAY);
513222671Simp		cf_outb_8(TF_SECTOR_COUNT, 1);
514222671Simp		cf_outb_8(TF_SECTOR_NUMBER, lba & 0xff);
515222671Simp		cf_outb_8(TF_CYL_LSB, (lba >> 8) & 0xff);
516222671Simp		cf_outb_8(TF_CYL_MSB, (lba >> 16) & 0xff);
517222671Simp		cf_outb_8(TF_DRV_HEAD, ((lba >> 24) & 0xff) | 0xe0);
518222671Simp		cf_outb_8(TF_COMMAND, cmd);
519222671Simp		break;
520222671Simp	case CF_16:
521222671Simp	default:
522222671Simp		while (cf_inb_16(TF_STATUS) & STATUS_BSY)
523194149Simp			DELAY(WAIT_DELAY);
524222671Simp		cf_outw_16(TF_SECTOR_COUNT, 1 | ((lba & 0xff) << 8));
525222671Simp		cf_outw_16(TF_CYL_LSB, ((lba >> 8) & 0xff) | (((lba >> 16) & 0xff) << 8));
526222671Simp		cf_outw_16(TF_DRV_HEAD, (((lba >> 24) & 0xff) | 0xe0) | (cmd << 8));
527222671Simp		break;
528194149Simp	}
529194149Simp
530204789Sjmallett	return (cf_wait_busy());
531194149Simp}
532194149Simp
533194149Simp/* ------------------------------------------------------------------- *
534194149Simp *                      cf_wait_busy()                                 *
535194149Simp * ------------------------------------------------------------------- *
536194149Simp *
537194149Simp * Wait until the drive finishes a given command and data is
538194149Simp * ready to be transferred. This is done by repeatedly checking
539204789Sjmallett * the BSY bit of the status register. When the controller is ready for
540204789Sjmallett * data transfer, it clears the BSY bit and sets the DRQ bit.
541194149Simp *
542204789Sjmallett * If the DF bit is ever set, we return error.
543204789Sjmallett *
544204789Sjmallett * This code originally spun on DRQ.  If that behavior turns out to be
545204789Sjmallett * necessary, a flag can be added or this function can be called
546204789Sjmallett * repeatedly as long as it is returning ENXIO.
547194149Simp */
548204789Sjmallettstatic int cf_wait_busy (void)
549194149Simp{
550194149Simp	uint8_t status;
551194149Simp
552194149Simp//#define OCTEON_VISUAL_CF_2 1
553194149Simp#ifdef OCTEON_VISUAL_CF_2
554194149Simp        static int where0 = 0;
555194149Simp
556194149Simp        octeon_led_run_wheel(&where0, 2);
557194149Simp#endif
558194149Simp
559222671Simp	switch (bus_type)
560222671Simp	{
561222671Simp	case CF_8:
562222671Simp	case CF_TRUE_IDE_8:
563222671Simp		status = cf_inb_8(TF_STATUS);
564204789Sjmallett		while ((status & STATUS_BSY) == STATUS_BSY) {
565204789Sjmallett			if ((status & STATUS_DF) != 0) {
566204789Sjmallett				printf("%s: device fault (status=%x)\n", __func__, status);
567204789Sjmallett				return (EIO);
568204789Sjmallett			}
569194149Simp			DELAY(WAIT_DELAY);
570222671Simp			status = cf_inb_8(TF_STATUS);
571194149Simp		}
572222671Simp		break;
573222671Simp	case CF_16:
574222671Simp	default:
575222671Simp		status = cf_inb_16(TF_STATUS);
576204789Sjmallett		while ((status & STATUS_BSY) == STATUS_BSY) {
577204789Sjmallett			if ((status & STATUS_DF) != 0) {
578204789Sjmallett				printf("%s: device fault (status=%x)\n", __func__, status);
579204789Sjmallett				return (EIO);
580204789Sjmallett			}
581194149Simp			DELAY(WAIT_DELAY);
582222671Simp			status = cf_inb_16(TF_STATUS);
583194149Simp		}
584222671Simp		break;
585194149Simp	}
586226020Smarcel
587226020Smarcel	/* DRQ is only for when read data is actually available; check BSY */
588226020Smarcel	/* Some vendors do assert DRQ, but not all. Check BSY instead. */
589226020Smarcel	if (status & STATUS_BSY) {
590204789Sjmallett		printf("%s: device not ready (status=%x)\n", __func__, status);
591204789Sjmallett		return (ENXIO);
592204789Sjmallett	}
593194149Simp
594194149Simp#ifdef OCTEON_VISUAL_CF_2
595194149Simp        octeon_led_write_char(2, ' ');
596194149Simp#endif
597204789Sjmallett	return (0);
598194149Simp}
599194149Simp
600194149Simp/* ------------------------------------------------------------------- *
601194149Simp *                      cf_swap_ascii()                                *
602194149Simp * ------------------------------------------------------------------- *
603194149Simp *
604194149Simp * The ascii string returned by the controller specifying
605194149Simp * the model of the drive is byte-swaped. This routine
606194149Simp * corrects the byte ordering.
607194149Simp *
608194149Simp */
609194149Simpstatic void cf_swap_ascii (unsigned char str1[], char str2[])
610194149Simp{
611194149Simp	int i;
612194149Simp
613222671Simp	for(i = 0; i < MODEL_STR_SIZE; i++)
614222671Simp		str2[i] = str1[i ^ 1];
615194149Simp}
616194149Simp
617194149Simp
618194149Simp/* ------------------------------------------------------------------- *
619194149Simp *                      cf_probe()                                     *
620194149Simp * ------------------------------------------------------------------- */
621194149Simp
622194149Simpstatic int cf_probe (device_t dev)
623194149Simp{
624222671Simp    	if (octeon_is_simulation())
625222671Simp		return (ENXIO);
626194149Simp
627194149Simp	if (device_get_unit(dev) != 0) {
628194149Simp                panic("can't attach more devices\n");
629194149Simp        }
630194149Simp
631194149Simp        device_set_desc(dev, "Octeon Compact Flash Driver");
632194149Simp
633204789Sjmallett	return (cf_cmd_identify());
634194149Simp}
635194149Simp
636194149Simp/* ------------------------------------------------------------------- *
637194149Simp *                      cf_identify()                                  *
638194149Simp * ------------------------------------------------------------------- *
639194149Simp *
640194149Simp * Find the bootbus region for the CF to determine
641194149Simp * 16 or 8 bit and check to see if device is
642194149Simp * inserted.
643194149Simp *
644194149Simp */
645194149Simpstatic void cf_identify (driver_t *drv, device_t parent)
646194149Simp{
647226020Smarcel	int bus_region;
648194149Simp	int count = 0;
649226020Smarcel	cvmx_mio_boot_reg_cfgx_t cfg;
650194149Simp
651226020Smarcel	uint64_t phys_base = octeon_bootinfo->compact_flash_common_base_addr;
652226020Smarcel
653210311Sjmallett    	if (octeon_is_simulation())
654194173Simp		return;
655194149Simp
656226020Smarcel	base_addr = cvmx_phys_to_ptr(phys_base);
657194149Simp
658194149Simp        for (bus_region = 0; bus_region < 8; bus_region++)
659194149Simp        {
660210311Sjmallett                cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(bus_region));
661226020Smarcel                if (cfg.s.base == phys_base >> 16)
662194149Simp                {
663226020Smarcel			if (cvmx_sysinfo_get()->compact_flash_attribute_base_addr == 0)
664222671Simp				bus_type = CF_TRUE_IDE_8;
665222671Simp			else
666222671Simp				bus_type = (cfg.s.width) ? CF_16 : CF_8;
667222671Simp                        printf("Compact flash found in bootbus region %d (%s).\n", bus_region, cf_type[bus_type]);
668194149Simp                        break;
669194149Simp                }
670194149Simp        }
671194149Simp
672222671Simp	switch (bus_type)
673222671Simp	{
674222671Simp	case CF_8:
675222671Simp	case CF_TRUE_IDE_8:
676194149Simp		/* Check if CF is inserted */
677222671Simp		while (cf_inb_8(TF_STATUS) & STATUS_BSY) {
678222671Simp			if ((count++) == NR_TRIES ) {
679194149Simp				printf("Compact Flash not present\n");
680194149Simp				return;
681194149Simp                	}
682194149Simp			DELAY(WAIT_DELAY);
683194149Simp        	}
684222671Simp		break;
685222671Simp	case CF_16:
686222671Simp	default:
687194149Simp		/* Check if CF is inserted */
688222671Simp		while (cf_inb_16(TF_STATUS) & STATUS_BSY) {
689222671Simp			if ((count++) == NR_TRIES ) {
690194149Simp				printf("Compact Flash not present\n");
691194149Simp				return;
692194149Simp                	}
693194149Simp			DELAY(WAIT_DELAY);
694194149Simp        	}
695222671Simp		break;
696194149Simp	}
697194149Simp
698194149Simp	BUS_ADD_CHILD(parent, 0, "cf", 0);
699194149Simp}
700194149Simp
701194149Simp
702194149Simp/* ------------------------------------------------------------------- *
703194149Simp *                      cf_attach_geom()                               *
704194149Simp * ------------------------------------------------------------------- */
705194149Simp
706194149Simpstatic int cf_attach_geom (void *arg, int flag)
707194149Simp{
708194149Simp	struct cf_priv *cf_priv;
709194149Simp
710194149Simp	cf_priv = (struct cf_priv *) arg;
711194149Simp	cf_priv->cf_geom = g_new_geomf(&g_cf_class, "cf%d", device_get_unit(cf_priv->dev));
712194149Simp	cf_priv->cf_provider = g_new_providerf(cf_priv->cf_geom, cf_priv->cf_geom->name);
713194149Simp	cf_priv->cf_geom->softc = cf_priv;
714194149Simp        g_error_provider(cf_priv->cf_provider, 0);
715194149Simp
716194149Simp        return (0);
717194149Simp}
718194149Simp
719194149Simp/* ------------------------------------------------------------------- *
720194149Simp *                      cf_attach_geom()                               *
721194149Simp * ------------------------------------------------------------------- */
722194149Simpstatic void cf_attach_geom_proxy (void *arg, int flag)
723194149Simp{
724222671Simp	cf_attach_geom(arg, flag);
725194149Simp}
726194149Simp
727194149Simp
728194149Simp
729194149Simp/* ------------------------------------------------------------------- *
730194149Simp *                      cf_attach()                                    *
731194149Simp * ------------------------------------------------------------------- */
732194149Simp
733194149Simpstatic int cf_attach (device_t dev)
734194149Simp{
735194149Simp	struct cf_priv *cf_priv;
736194149Simp
737222671Simp    	if (octeon_is_simulation())
738222671Simp		return (ENXIO);
739194149Simp
740194149Simp	cf_priv = device_get_softc(dev);
741194149Simp	cf_priv->dev = dev;
742194149Simp	cf_priv->drive_param = &drive_param;
743194149Simp
744194149Simp	g_post_event(cf_attach_geom_proxy, cf_priv, M_WAITOK, NULL);
745194149Simp	bioq_init(&cf_priv->cf_bq);
746194149Simp
747194149Simp        return 0;
748194149Simp}
749194149Simp
750194149Simp
751194149Simpstatic device_method_t cf_methods[] = {
752194149Simp        /* Device interface */
753194149Simp        DEVMETHOD(device_probe,         cf_probe),
754194149Simp        DEVMETHOD(device_identify,      cf_identify),
755194149Simp        DEVMETHOD(device_attach,        cf_attach),
756194149Simp        DEVMETHOD(device_detach,        bus_generic_detach),
757194149Simp        DEVMETHOD(device_shutdown,      bus_generic_shutdown),
758194149Simp
759194149Simp        { 0, 0 }
760194149Simp};
761194149Simp
762194149Simpstatic driver_t cf_driver = {
763194149Simp        "cf",
764194149Simp	cf_methods,
765194149Simp	sizeof(struct cf_priv)
766194149Simp};
767194149Simp
768194149Simpstatic devclass_t cf_devclass;
769194149Simp
770194149SimpDRIVER_MODULE(cf, nexus, cf_driver, cf_devclass, 0, 0);
771