octeon_ebt3000_cf.c revision 210311
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 210311 2010-07-20 19:25:11Z jmallett $");
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 */
107194149Simpint	bus_width;
108194149Simpvoid	*base_addr;
109194149Simp
110194149Simp/* Device softc */
111194149Simpstruct cf_priv {
112194149Simp
113194149Simp	device_t dev;
114194149Simp	struct drive_param *drive_param;
115194149Simp
116194149Simp	struct bio_queue_head cf_bq;
117194149Simp	struct g_geom *cf_geom;
118194149Simp	struct g_provider *cf_provider;
119194149Simp
120194149Simp};
121194149Simp
122194149Simp/* Device parameters */
123194149Simpstruct drive_param{
124194149Simp	union {
125194149Simp		char buf[SECTOR_SIZE];
126210311Sjmallett		struct ata_params driveid;
127194149Simp	} u;
128194149Simp
129194149Simp	char model[MODEL_STR_SIZE];
130194149Simp	uint32_t nr_sectors;
131194149Simp	uint16_t sector_size;
132194149Simp	uint16_t heads;
133194149Simp	uint16_t tracks;
134194149Simp	uint16_t sec_track;
135194149Simp
136194149Simp} drive_param;
137194149Simp
138194149Simp/* GEOM class implementation */
139194149Simpstatic g_access_t       cf_access;
140194149Simpstatic g_start_t        cf_start;
141194149Simpstatic g_ioctl_t        cf_ioctl;
142194149Simp
143194149Simpstruct g_class g_cf_class = {
144194149Simp        .name =         "CF",
145194149Simp        .version =      G_VERSION,
146194149Simp        .start =        cf_start,
147194149Simp        .access =       cf_access,
148194149Simp        .ioctl =        cf_ioctl,
149194149Simp};
150194149Simp
151194149Simp/* Device methods */
152194149Simpstatic int	cf_probe(device_t);
153194149Simpstatic void	cf_identify(driver_t *, device_t);
154194149Simpstatic int	cf_attach(device_t);
155194149Simpstatic int	cf_attach_geom(void *, int);
156194149Simp
157194149Simp/* ATA methods */
158204789Sjmallettstatic int	cf_cmd_identify(void);
159204789Sjmallettstatic int	cf_cmd_write(uint32_t, uint32_t, void *);
160204789Sjmallettstatic int	cf_cmd_read(uint32_t, uint32_t, void *);
161204789Sjmallettstatic int	cf_wait_busy(void);
162204789Sjmallettstatic int	cf_send_cmd(uint32_t, uint8_t);
163194149Simpstatic void	cf_attach_geom_proxy(void *arg, int flag);
164194149Simp
165194149Simp/* Miscelenous */
166194149Simpstatic void	cf_swap_ascii(unsigned char[], char[]);
167194149Simp
168194149Simp
169194149Simp/* ------------------------------------------------------------------- *
170194149Simp *                      cf_access()                                    *
171194149Simp * ------------------------------------------------------------------- */
172194149Simpstatic int cf_access (struct g_provider *pp, int r, int w, int e)
173194149Simp{
174194149Simp
175194149Simp	pp->sectorsize = drive_param.sector_size;
176194149Simp        pp->stripesize = drive_param.heads * drive_param.sec_track * drive_param.sector_size;
177194149Simp        pp->mediasize  = pp->stripesize * drive_param.tracks;
178194149Simp
179194149Simp	return (0);
180194149Simp}
181194149Simp
182194149Simp
183194149Simp/* ------------------------------------------------------------------- *
184194149Simp *                      cf_start()                                     *
185194149Simp * ------------------------------------------------------------------- */
186194149Simpstatic void cf_start (struct bio *bp)
187194149Simp{
188204789Sjmallett	int error;
189204789Sjmallett
190194149Simp	/*
191194149Simp	* Handle actual I/O requests. The request is passed down through
192194149Simp	* the bio struct.
193194149Simp	*/
194194149Simp
195194149Simp	if(bp->bio_cmd & BIO_GETATTR) {
196194149Simp		if (g_handleattr_int(bp, "GEOM::fwsectors", drive_param.sec_track))
197194149Simp                        return;
198194149Simp                if (g_handleattr_int(bp, "GEOM::fwheads",   drive_param.heads))
199194149Simp                        return;
200194149Simp                g_io_deliver(bp, ENOIOCTL);
201194149Simp                return;
202194149Simp	}
203194149Simp
204194149Simp	if ((bp->bio_cmd & (BIO_READ | BIO_WRITE))) {
205194149Simp
206194149Simp		if (bp->bio_cmd & BIO_READ) {
207204789Sjmallett			error = cf_cmd_read(bp->bio_length / drive_param.sector_size,
208204789Sjmallett			    bp->bio_offset / drive_param.sector_size, bp->bio_data);
209194149Simp		} else if (bp->bio_cmd & BIO_WRITE) {
210204789Sjmallett			error = cf_cmd_write(bp->bio_length / drive_param.sector_size,
211204789Sjmallett			    bp->bio_offset/drive_param.sector_size, bp->bio_data);
212204789Sjmallett		} else {
213204789Sjmallett			printf("%s: unrecognized bio_cmd %x.\n", __func__, bp->bio_cmd);
214204789Sjmallett			error = ENOTSUP;
215194149Simp		}
216194149Simp
217204789Sjmallett		if (error != 0) {
218204789Sjmallett			g_io_deliver(bp, error);
219204789Sjmallett			return;
220204789Sjmallett		}
221204789Sjmallett
222194149Simp		bp->bio_resid = 0;
223194149Simp		bp->bio_completed = bp->bio_length;
224194149Simp		g_io_deliver(bp, 0);
225194149Simp	}
226194149Simp}
227194149Simp
228194149Simp
229194149Simpstatic int cf_ioctl (struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
230194149Simp{
231194149Simp    return (0);
232194149Simp}
233194149Simp
234194149Simp
235194149Simp/* ------------------------------------------------------------------- *
236194149Simp *                      cf_cmd_read()                                  *
237194149Simp * ------------------------------------------------------------------- *
238194149Simp *
239194149Simp *  Read nr_sectors from the device starting from start_sector.
240194149Simp */
241204789Sjmallettstatic int cf_cmd_read (uint32_t nr_sectors, uint32_t start_sector, void *buf)
242194149Simp{
243194149Simp	unsigned long lba;
244194149Simp	uint32_t count;
245194149Simp	uint16_t *ptr_16;
246194149Simp	uint8_t  *ptr_8;
247204789Sjmallett	int error;
248194149Simp
249194149Simp//#define OCTEON_VISUAL_CF_0 1
250194149Simp#ifdef OCTEON_VISUAL_CF_0
251194149Simp        octeon_led_write_char(0, 'R');
252194149Simp#endif
253194149Simp	ptr_8  = (uint8_t*)buf;
254194149Simp	ptr_16 = (uint16_t*)buf;
255194149Simp	lba = start_sector;
256194149Simp
257194149Simp
258194149Simp	while (nr_sectors--) {
259204789Sjmallett		error = cf_send_cmd(lba, CMD_READ_SECTOR);
260204789Sjmallett		if (error != 0) {
261204789Sjmallett			printf("%s: cf_send_cmd(CMD_READ_SECTOR) failed: %d\n", __func__, error);
262204789Sjmallett			return (error);
263204789Sjmallett		}
264194149Simp
265194149Simp		if (bus_width == 8) {
266194149Simp			volatile uint8_t *task_file = (volatile uint8_t*)base_addr;
267194149Simp        		volatile uint8_t dummy;
268194149Simp			for (count = 0; count < SECTOR_SIZE; count++) {
269194149Simp				*ptr_8++ = task_file[TF_DATA];
270194149Simp				if ((count & 0xf) == 0) dummy = task_file[TF_STATUS];
271194149Simp			}
272194149Simp		} else {
273194149Simp			volatile uint16_t *task_file = (volatile uint16_t*)base_addr;
274194149Simp        		volatile uint16_t dummy;
275194149Simp			for (count = 0; count < SECTOR_SIZE; count+=2) {
276194149Simp				uint16_t temp;
277194149Simp				temp = task_file[TF_DATA];
278194149Simp				*ptr_16++ = SWAP_SHORT(temp);
279194149Simp				if ((count & 0xf) == 0) dummy = task_file[TF_STATUS/2];
280194149Simp			}
281194149Simp		}
282194149Simp
283194149Simp		lba ++;
284194149Simp	}
285194149Simp#ifdef OCTEON_VISUAL_CF_0
286194149Simp        octeon_led_write_char(0, ' ');
287194149Simp#endif
288204789Sjmallett	return (0);
289194149Simp}
290194149Simp
291194149Simp
292194149Simp/* ------------------------------------------------------------------- *
293194149Simp *                      cf_cmd_write()                                 *
294194149Simp * ------------------------------------------------------------------- *
295194149Simp *
296194149Simp * Write nr_sectors to the device starting from start_sector.
297194149Simp */
298204789Sjmallettstatic int cf_cmd_write (uint32_t nr_sectors, uint32_t start_sector, void *buf)
299194149Simp{
300194149Simp	uint32_t lba;
301194149Simp	uint32_t count;
302194149Simp	uint16_t *ptr_16;
303194149Simp	uint8_t  *ptr_8;
304204789Sjmallett	int error;
305194149Simp
306194149Simp//#define OCTEON_VISUAL_CF_1 1
307194149Simp#ifdef OCTEON_VISUAL_CF_1
308194149Simp        octeon_led_write_char(1, 'W');
309194149Simp#endif
310194149Simp	lba = start_sector;
311194149Simp	ptr_8  = (uint8_t*)buf;
312194149Simp	ptr_16 = (uint16_t*)buf;
313194149Simp
314194149Simp	while (nr_sectors--) {
315204789Sjmallett		error = cf_send_cmd(lba, CMD_WRITE_SECTOR);
316204789Sjmallett		if (error != 0) {
317204789Sjmallett			printf("%s: cf_send_cmd(CMD_WRITE_SECTOR) failed: %d\n", __func__, error);
318204789Sjmallett			return (error);
319204789Sjmallett		}
320194149Simp
321194149Simp		if (bus_width == 8) {
322194149Simp			volatile uint8_t *task_file;
323194149Simp        		volatile uint8_t dummy;
324194149Simp
325194149Simp			task_file = (volatile uint8_t *) base_addr;
326194149Simp			for (count = 0; count < SECTOR_SIZE; count++) {
327194149Simp				task_file[TF_DATA] =  *ptr_8++;
328194149Simp				if ((count & 0xf) == 0) dummy = task_file[TF_STATUS];
329194149Simp			}
330194149Simp		} else {
331194149Simp			volatile uint16_t *task_file;
332194149Simp        		volatile uint16_t dummy;
333194149Simp
334194149Simp			task_file = (volatile uint16_t *) base_addr;
335194149Simp			for (count = 0; count < SECTOR_SIZE; count+=2) {
336194149Simp				uint16_t temp = *ptr_16++;
337194149Simp				task_file[TF_DATA] =  SWAP_SHORT(temp);
338194149Simp				if ((count & 0xf) == 0) dummy = task_file[TF_STATUS/2];
339194149Simp			}
340194149Simp		}
341194149Simp
342194149Simp		lba ++;
343194149Simp	}
344194149Simp#ifdef OCTEON_VISUAL_CF_1
345194149Simp        octeon_led_write_char(1, ' ');
346194149Simp#endif
347204789Sjmallett	return (0);
348194149Simp}
349194149Simp
350194149Simp
351194149Simp/* ------------------------------------------------------------------- *
352194149Simp *                      cf_cmd_identify()                              *
353194149Simp * ------------------------------------------------------------------- *
354194149Simp *
355194149Simp * Read parameters and other information from the drive and store
356194149Simp * it in the drive_param structure
357194149Simp *
358194149Simp */
359204789Sjmallettstatic int cf_cmd_identify (void)
360194149Simp{
361194149Simp	int count;
362194149Simp	uint8_t status;
363204789Sjmallett	int error;
364194149Simp
365194149Simp	if (bus_width == 8) {
366194149Simp        	volatile uint8_t *task_file;
367194149Simp
368194149Simp        	task_file = (volatile uint8_t *) base_addr;
369194149Simp
370194149Simp		while ((status = task_file[TF_STATUS]) & STATUS_BSY) {
371194149Simp			DELAY(WAIT_DELAY);
372194149Simp        	}
373194149Simp
374194149Simp        	task_file[TF_SECTOR_COUNT]  = 0;
375194149Simp        	task_file[TF_SECTOR_NUMBER] = 0;
376194149Simp        	task_file[TF_CYL_LSB]  = 0;
377194149Simp        	task_file[TF_CYL_MSB]  = 0;
378194149Simp        	task_file[TF_DRV_HEAD] = 0;
379194149Simp        	task_file[TF_COMMAND]  = CMD_IDENTIFY;
380194149Simp
381204789Sjmallett		error = cf_wait_busy();
382204789Sjmallett		if (error == 0) {
383204789Sjmallett			for (count = 0; count < SECTOR_SIZE; count++)
384204789Sjmallett				drive_param.u.buf[count] = task_file[TF_DATA];
385204789Sjmallett		}
386194149Simp	} else {
387194149Simp		volatile uint16_t *task_file;
388194149Simp
389194149Simp		task_file = (volatile uint16_t *) base_addr;
390194149Simp
391194149Simp		while ((status = (task_file[TF_STATUS/2]>>8)) & STATUS_BSY) {
392194149Simp			DELAY(WAIT_DELAY);
393194149Simp		}
394194149Simp
395194149Simp		task_file[TF_SECTOR_COUNT/2]  = 0; /* this includes TF_SECTOR_NUMBER */
396194149Simp		task_file[TF_CYL_LSB/2]  = 0; /* this includes TF_CYL_MSB */
397194149Simp		task_file[TF_DRV_HEAD/2] = 0 | (CMD_IDENTIFY<<8); /* this includes TF_COMMAND */
398194149Simp
399204789Sjmallett		error = cf_wait_busy();
400204789Sjmallett		if (error == 0) {
401204789Sjmallett			for (count = 0; count < SECTOR_SIZE; count+=2) {
402204789Sjmallett				uint16_t temp;
403204789Sjmallett				temp = task_file[TF_DATA];
404204789Sjmallett
405204789Sjmallett				/* endianess will be swapped below */
406204789Sjmallett				drive_param.u.buf[count]   = (temp & 0xff);
407204789Sjmallett				drive_param.u.buf[count+1] = (temp & 0xff00)>>8;
408204789Sjmallett			}
409194149Simp		}
410194149Simp	}
411204789Sjmallett	if (error != 0) {
412204789Sjmallett		printf("%s: identify failed: %d\n", __func__, error);
413204789Sjmallett		return (error);
414204789Sjmallett	}
415194149Simp
416194149Simp	cf_swap_ascii(drive_param.u.driveid.model, drive_param.model);
417194149Simp
418194149Simp	drive_param.sector_size =  512;   //=  SWAP_SHORT (drive_param.u.driveid.sector_bytes);
419210311Sjmallett	drive_param.heads 	=  SWAP_SHORT (drive_param.u.driveid.current_heads);
420210311Sjmallett	drive_param.tracks	=  SWAP_SHORT (drive_param.u.driveid.current_cylinders);
421210311Sjmallett	drive_param.sec_track   =  SWAP_SHORT (drive_param.u.driveid.current_sectors);
422210311Sjmallett	drive_param.nr_sectors  = (uint32_t)SWAP_SHORT (drive_param.u.driveid.lba_size_1) |
423210311Sjmallett	    ((uint32_t)SWAP_SHORT (drive_param.u.driveid.lba_size_2));
424194149Simp
425204789Sjmallett	return (0);
426194149Simp}
427194149Simp
428194149Simp
429194149Simp/* ------------------------------------------------------------------- *
430194149Simp *                      cf_send_cmd()                                  *
431194149Simp * ------------------------------------------------------------------- *
432194149Simp *
433194149Simp * Send command to read/write one sector specified by lba.
434194149Simp *
435194149Simp */
436204789Sjmallettstatic int cf_send_cmd (uint32_t lba, uint8_t cmd)
437194149Simp{
438194149Simp	uint8_t status;
439194149Simp
440194149Simp	if (bus_width == 8) {
441194149Simp		volatile uint8_t *task_file;
442194149Simp
443194149Simp		task_file = (volatile uint8_t *) base_addr;
444194149Simp
445194149Simp		while ( (status = task_file[TF_STATUS]) & STATUS_BSY) {
446194149Simp			DELAY(WAIT_DELAY);
447194149Simp		}
448194149Simp
449194149Simp		task_file[TF_SECTOR_COUNT]  = 1;
450194149Simp		task_file[TF_SECTOR_NUMBER] = (lba & 0xff);
451194149Simp		task_file[TF_CYL_LSB]  =  ((lba >> 8) & 0xff);
452194149Simp		task_file[TF_CYL_MSB]  =  ((lba >> 16) & 0xff);
453194149Simp		task_file[TF_DRV_HEAD] =  ((lba >> 24) & 0xff) | 0xe0;
454194149Simp		task_file[TF_COMMAND]  =  cmd;
455194149Simp
456194149Simp	} else {
457194149Simp		volatile uint16_t *task_file;
458194149Simp
459194149Simp		task_file = (volatile uint16_t *) base_addr;
460194149Simp
461194149Simp		while ( (status = (task_file[TF_STATUS/2]>>8)) & STATUS_BSY) {
462194149Simp			DELAY(WAIT_DELAY);
463194149Simp		}
464194149Simp
465194149Simp		task_file[TF_SECTOR_COUNT/2]  = 1 | ((lba & 0xff) << 8);
466194149Simp		task_file[TF_CYL_LSB/2]  =  ((lba >> 8) & 0xff) | (((lba >> 16) & 0xff) << 8);
467194149Simp		task_file[TF_DRV_HEAD/2] =  (((lba >> 24) & 0xff) | 0xe0) | (cmd << 8);
468194149Simp
469194149Simp	}
470194149Simp
471204789Sjmallett	return (cf_wait_busy());
472194149Simp}
473194149Simp
474194149Simp/* ------------------------------------------------------------------- *
475194149Simp *                      cf_wait_busy()                                 *
476194149Simp * ------------------------------------------------------------------- *
477194149Simp *
478194149Simp * Wait until the drive finishes a given command and data is
479194149Simp * ready to be transferred. This is done by repeatedly checking
480204789Sjmallett * the BSY bit of the status register. When the controller is ready for
481204789Sjmallett * data transfer, it clears the BSY bit and sets the DRQ bit.
482194149Simp *
483204789Sjmallett * If the DF bit is ever set, we return error.
484204789Sjmallett *
485204789Sjmallett * This code originally spun on DRQ.  If that behavior turns out to be
486204789Sjmallett * necessary, a flag can be added or this function can be called
487204789Sjmallett * repeatedly as long as it is returning ENXIO.
488194149Simp */
489204789Sjmallettstatic int cf_wait_busy (void)
490194149Simp{
491194149Simp	uint8_t status;
492194149Simp
493194149Simp//#define OCTEON_VISUAL_CF_2 1
494194149Simp#ifdef OCTEON_VISUAL_CF_2
495194149Simp        static int where0 = 0;
496194149Simp
497194149Simp        octeon_led_run_wheel(&where0, 2);
498194149Simp#endif
499194149Simp
500194149Simp	if (bus_width == 8) {
501194149Simp		volatile uint8_t *task_file;
502194149Simp		task_file = (volatile uint8_t *)base_addr;
503194149Simp
504194149Simp		status = task_file[TF_STATUS];
505204789Sjmallett		while ((status & STATUS_BSY) == STATUS_BSY) {
506204789Sjmallett			if ((status & STATUS_DF) != 0) {
507204789Sjmallett				printf("%s: device fault (status=%x)\n", __func__, status);
508204789Sjmallett				return (EIO);
509204789Sjmallett			}
510194149Simp			DELAY(WAIT_DELAY);
511194149Simp			status = task_file[TF_STATUS];
512194149Simp		}
513194149Simp	} else {
514194149Simp		volatile uint16_t *task_file;
515194149Simp		task_file = (volatile uint16_t *)base_addr;
516194149Simp
517194149Simp		status = task_file[TF_STATUS/2]>>8;
518204789Sjmallett		while ((status & STATUS_BSY) == STATUS_BSY) {
519204789Sjmallett			if ((status & STATUS_DF) != 0) {
520204789Sjmallett				printf("%s: device fault (status=%x)\n", __func__, status);
521204789Sjmallett				return (EIO);
522204789Sjmallett			}
523194149Simp			DELAY(WAIT_DELAY);
524194149Simp			status = (uint8_t)(task_file[TF_STATUS/2]>>8);
525194149Simp		}
526194149Simp	}
527204789Sjmallett	if ((status & STATUS_DRQ) == 0) {
528204789Sjmallett		printf("%s: device not ready (status=%x)\n", __func__, status);
529204789Sjmallett		return (ENXIO);
530204789Sjmallett	}
531194149Simp
532194149Simp#ifdef OCTEON_VISUAL_CF_2
533194149Simp        octeon_led_write_char(2, ' ');
534194149Simp#endif
535204789Sjmallett	return (0);
536194149Simp}
537194149Simp
538194149Simp/* ------------------------------------------------------------------- *
539194149Simp *                      cf_swap_ascii()                                *
540194149Simp * ------------------------------------------------------------------- *
541194149Simp *
542194149Simp * The ascii string returned by the controller specifying
543194149Simp * the model of the drive is byte-swaped. This routine
544194149Simp * corrects the byte ordering.
545194149Simp *
546194149Simp */
547194149Simpstatic void cf_swap_ascii (unsigned char str1[], char str2[])
548194149Simp{
549194149Simp	int i;
550194149Simp
551194149Simp	for(i = 0; i < MODEL_STR_SIZE; i++) {
552194149Simp            str2[i] = str1[i^1];
553194149Simp        }
554194149Simp}
555194149Simp
556194149Simp
557194149Simp/* ------------------------------------------------------------------- *
558194149Simp *                      cf_probe()                                     *
559194149Simp * ------------------------------------------------------------------- */
560194149Simp
561194149Simpstatic int cf_probe (device_t dev)
562194149Simp{
563210311Sjmallett    	if (octeon_is_simulation()) return 1;
564194149Simp
565194149Simp	if (device_get_unit(dev) != 0) {
566194149Simp                panic("can't attach more devices\n");
567194149Simp        }
568194149Simp
569194149Simp        device_set_desc(dev, "Octeon Compact Flash Driver");
570194149Simp
571204789Sjmallett	return (cf_cmd_identify());
572194149Simp}
573194149Simp
574194149Simp/* ------------------------------------------------------------------- *
575194149Simp *                      cf_identify()                                  *
576194149Simp * ------------------------------------------------------------------- *
577194149Simp *
578194149Simp * Find the bootbus region for the CF to determine
579194149Simp * 16 or 8 bit and check to see if device is
580194149Simp * inserted.
581194149Simp *
582194149Simp */
583194149Simpstatic void cf_identify (driver_t *drv, device_t parent)
584194149Simp{
585194149Simp	uint8_t status;
586194149Simp        int bus_region;
587194149Simp	int count = 0;
588210311Sjmallett        cvmx_mio_boot_reg_cfgx_t cfg;
589194149Simp
590210311Sjmallett    	if (octeon_is_simulation())
591194173Simp		return;
592194149Simp
593210311Sjmallett	base_addr = cvmx_phys_to_ptr(octeon_bootinfo->compact_flash_common_base_addr);
594194149Simp
595194149Simp        for (bus_region = 0; bus_region < 8; bus_region++)
596194149Simp        {
597210311Sjmallett                cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(bus_region));
598210311Sjmallett                if (cfg.s.base == octeon_bootinfo->compact_flash_common_base_addr >> 16)
599194149Simp                {
600210311Sjmallett                        bus_width = (cfg.s.width) ? 16: 8;
601194149Simp                        printf("Compact flash found in bootbus region %d (%d bit).\n", bus_region, bus_width);
602194149Simp                        break;
603194149Simp                }
604194149Simp        }
605194149Simp
606194149Simp	if (bus_width == 8) {
607194149Simp		volatile uint8_t *task_file;
608194149Simp		task_file = (volatile uint8_t *) base_addr;
609194149Simp		/* Check if CF is inserted */
610194149Simp		while ( (status = task_file[TF_STATUS]) & STATUS_BSY){
611194149Simp			if ((count++) == NR_TRIES )     {
612194149Simp				printf("Compact Flash not present\n");
613194149Simp				return;
614194149Simp                	}
615194149Simp			DELAY(WAIT_DELAY);
616194149Simp        	}
617194149Simp	} else {
618194149Simp		volatile uint16_t *task_file;
619194149Simp		task_file = (volatile uint16_t *) base_addr;
620194149Simp		/* Check if CF is inserted */
621194149Simp		while ( (status = (task_file[TF_STATUS/2]>>8)) & STATUS_BSY){
622194149Simp			if ((count++) == NR_TRIES )     {
623194149Simp				printf("Compact Flash not present\n");
624194149Simp				return;
625194149Simp                	}
626194149Simp			DELAY(WAIT_DELAY);
627194149Simp        	}
628194149Simp	}
629194149Simp
630194149Simp	BUS_ADD_CHILD(parent, 0, "cf", 0);
631194149Simp}
632194149Simp
633194149Simp
634194149Simp/* ------------------------------------------------------------------- *
635194149Simp *                      cf_attach_geom()                               *
636194149Simp * ------------------------------------------------------------------- */
637194149Simp
638194149Simpstatic int cf_attach_geom (void *arg, int flag)
639194149Simp{
640194149Simp	struct cf_priv *cf_priv;
641194149Simp
642194149Simp	cf_priv = (struct cf_priv *) arg;
643194149Simp	cf_priv->cf_geom = g_new_geomf(&g_cf_class, "cf%d", device_get_unit(cf_priv->dev));
644194149Simp	cf_priv->cf_provider = g_new_providerf(cf_priv->cf_geom, cf_priv->cf_geom->name);
645194149Simp	cf_priv->cf_geom->softc = cf_priv;
646194149Simp        g_error_provider(cf_priv->cf_provider, 0);
647194149Simp
648194149Simp        return (0);
649194149Simp}
650194149Simp
651194149Simp/* ------------------------------------------------------------------- *
652194149Simp *                      cf_attach_geom()                               *
653194149Simp * ------------------------------------------------------------------- */
654194149Simpstatic void cf_attach_geom_proxy (void *arg, int flag)
655194149Simp{
656194149Simp    cf_attach_geom(arg, flag);
657194149Simp}
658194149Simp
659194149Simp
660194149Simp
661194149Simp/* ------------------------------------------------------------------- *
662194149Simp *                      cf_attach()                                    *
663194149Simp * ------------------------------------------------------------------- */
664194149Simp
665194149Simpstatic int cf_attach (device_t dev)
666194149Simp{
667194149Simp	struct cf_priv *cf_priv;
668194149Simp
669210311Sjmallett    	if (octeon_is_simulation()) return 1;
670194149Simp
671194149Simp	cf_priv = device_get_softc(dev);
672194149Simp	cf_priv->dev = dev;
673194149Simp	cf_priv->drive_param = &drive_param;
674194149Simp
675194149Simp	g_post_event(cf_attach_geom_proxy, cf_priv, M_WAITOK, NULL);
676194149Simp	bioq_init(&cf_priv->cf_bq);
677194149Simp
678194149Simp        return 0;
679194149Simp}
680194149Simp
681194149Simp
682194149Simpstatic device_method_t cf_methods[] = {
683194149Simp        /* Device interface */
684194149Simp        DEVMETHOD(device_probe,         cf_probe),
685194149Simp        DEVMETHOD(device_identify,      cf_identify),
686194149Simp        DEVMETHOD(device_attach,        cf_attach),
687194149Simp        DEVMETHOD(device_detach,        bus_generic_detach),
688194149Simp        DEVMETHOD(device_shutdown,      bus_generic_shutdown),
689194149Simp
690194149Simp        { 0, 0 }
691194149Simp};
692194149Simp
693194149Simpstatic driver_t cf_driver = {
694194149Simp        "cf",
695194149Simp	cf_methods,
696194149Simp	sizeof(struct cf_priv)
697194149Simp};
698194149Simp
699194149Simpstatic devclass_t cf_devclass;
700194149Simp
701194149SimpDRIVER_MODULE(cf, nexus, cf_driver, cf_devclass, 0, 0);
702194149Simp
703