1 /***********************license start***************
2 * Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * * Neither the name of Cavium Networks nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25 * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26 * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31 * POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT
32 * OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
35 * For any questions regarding licensing please contact marketing@caviumnetworks.com
37 ***********************license end**************************************/
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/sysctl.h>
53 #include <sys/kernel.h>
54 #include <sys/module.h>
56 #include <sys/power.h>
59 #include <sys/timetc.h>
60 #include <sys/malloc.h>
62 #include <geom/geom.h>
64 #include <machine/clock.h>
65 #include <machine/locore.h>
66 #include <machine/md_var.h>
67 #include <machine/cpuregs.h>
69 #include <mips/cavium/octeon_pcmap_regs.h>
71 #include <contrib/octeon-sdk/cvmx.h>
74 #define CMD_READ_SECTOR 0x20
75 #define CMD_WRITE_SECTOR 0x30
76 #define CMD_IDENTIFY 0xEC
78 /* The ATA Task File */
81 #define TF_PRECOMP 0x01
82 #define TF_SECTOR_COUNT 0x02
83 #define TF_SECTOR_NUMBER 0x03
84 #define TF_CYL_LSB 0x04
85 #define TF_CYL_MSB 0x05
86 #define TF_DRV_HEAD 0x06
87 #define TF_STATUS 0x07
88 #define TF_COMMAND 0x07
91 #define STATUS_BSY 0x80 /* Drive is busy */
92 #define STATUS_RDY 0x40 /* Drive is ready */
93 #define STATUS_DF 0x20 /* Device fault */
94 #define STATUS_DRQ 0x08 /* Data can be transferred */
97 #define SECTOR_SIZE 512
98 #define WAIT_DELAY 1000
100 #define SWAP_SHORT(x) ((x << 8) | (x >> 8))
101 #define MODEL_STR_SIZE 40
104 extern cvmx_bootinfo_t *octeon_bootinfo;
114 struct drive_param *drive_param;
116 struct bio_queue_head cf_bq;
117 struct g_geom *cf_geom;
118 struct g_provider *cf_provider;
122 /* Device parameters */
125 char buf[SECTOR_SIZE];
126 struct ata_params driveid;
129 char model[MODEL_STR_SIZE];
131 uint16_t sector_size;
138 /* GEOM class implementation */
139 static g_access_t cf_access;
140 static g_start_t cf_start;
141 static g_ioctl_t cf_ioctl;
143 struct g_class g_cf_class = {
145 .version = G_VERSION,
151 DECLARE_GEOM_CLASS(g_cf_class, g_cf);
154 static int cf_probe(device_t);
155 static void cf_identify(driver_t *, device_t);
156 static int cf_attach(device_t);
157 static int cf_attach_geom(void *, int);
160 static int cf_cmd_identify(void);
161 static int cf_cmd_write(uint32_t, uint32_t, void *);
162 static int cf_cmd_read(uint32_t, uint32_t, void *);
163 static int cf_wait_busy(void);
164 static int cf_send_cmd(uint32_t, uint8_t);
165 static void cf_attach_geom_proxy(void *arg, int flag);
168 static void cf_swap_ascii(unsigned char[], char[]);
171 /* ------------------------------------------------------------------- *
173 * ------------------------------------------------------------------- */
174 static int cf_access (struct g_provider *pp, int r, int w, int e)
177 pp->sectorsize = drive_param.sector_size;
178 pp->stripesize = drive_param.heads * drive_param.sec_track * drive_param.sector_size;
179 pp->mediasize = pp->stripesize * drive_param.tracks;
185 /* ------------------------------------------------------------------- *
187 * ------------------------------------------------------------------- */
188 static void cf_start (struct bio *bp)
193 * Handle actual I/O requests. The request is passed down through
197 if(bp->bio_cmd & BIO_GETATTR) {
198 if (g_handleattr_int(bp, "GEOM::fwsectors", drive_param.sec_track))
200 if (g_handleattr_int(bp, "GEOM::fwheads", drive_param.heads))
202 g_io_deliver(bp, ENOIOCTL);
206 if ((bp->bio_cmd & (BIO_READ | BIO_WRITE))) {
208 if (bp->bio_cmd & BIO_READ) {
209 error = cf_cmd_read(bp->bio_length / drive_param.sector_size,
210 bp->bio_offset / drive_param.sector_size, bp->bio_data);
211 } else if (bp->bio_cmd & BIO_WRITE) {
212 error = cf_cmd_write(bp->bio_length / drive_param.sector_size,
213 bp->bio_offset/drive_param.sector_size, bp->bio_data);
215 printf("%s: unrecognized bio_cmd %x.\n", __func__, bp->bio_cmd);
220 g_io_deliver(bp, error);
225 bp->bio_completed = bp->bio_length;
231 static int cf_ioctl (struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
237 /* ------------------------------------------------------------------- *
239 * ------------------------------------------------------------------- *
241 * Read nr_sectors from the device starting from start_sector.
243 static int cf_cmd_read (uint32_t nr_sectors, uint32_t start_sector, void *buf)
251 //#define OCTEON_VISUAL_CF_0 1
252 #ifdef OCTEON_VISUAL_CF_0
253 octeon_led_write_char(0, 'R');
255 ptr_8 = (uint8_t*)buf;
256 ptr_16 = (uint16_t*)buf;
260 while (nr_sectors--) {
261 error = cf_send_cmd(lba, CMD_READ_SECTOR);
263 printf("%s: cf_send_cmd(CMD_READ_SECTOR) failed: %d\n", __func__, error);
267 if (bus_width == 8) {
268 volatile uint8_t *task_file = (volatile uint8_t*)base_addr;
269 volatile uint8_t dummy;
270 for (count = 0; count < SECTOR_SIZE; count++) {
271 *ptr_8++ = task_file[TF_DATA];
272 if ((count & 0xf) == 0) dummy = task_file[TF_STATUS];
275 volatile uint16_t *task_file = (volatile uint16_t*)base_addr;
276 volatile uint16_t dummy;
277 for (count = 0; count < SECTOR_SIZE; count+=2) {
279 temp = task_file[TF_DATA];
280 *ptr_16++ = SWAP_SHORT(temp);
281 if ((count & 0xf) == 0) dummy = task_file[TF_STATUS/2];
287 #ifdef OCTEON_VISUAL_CF_0
288 octeon_led_write_char(0, ' ');
294 /* ------------------------------------------------------------------- *
296 * ------------------------------------------------------------------- *
298 * Write nr_sectors to the device starting from start_sector.
300 static int cf_cmd_write (uint32_t nr_sectors, uint32_t start_sector, void *buf)
308 //#define OCTEON_VISUAL_CF_1 1
309 #ifdef OCTEON_VISUAL_CF_1
310 octeon_led_write_char(1, 'W');
313 ptr_8 = (uint8_t*)buf;
314 ptr_16 = (uint16_t*)buf;
316 while (nr_sectors--) {
317 error = cf_send_cmd(lba, CMD_WRITE_SECTOR);
319 printf("%s: cf_send_cmd(CMD_WRITE_SECTOR) failed: %d\n", __func__, error);
323 if (bus_width == 8) {
324 volatile uint8_t *task_file;
325 volatile uint8_t dummy;
327 task_file = (volatile uint8_t *) base_addr;
328 for (count = 0; count < SECTOR_SIZE; count++) {
329 task_file[TF_DATA] = *ptr_8++;
330 if ((count & 0xf) == 0) dummy = task_file[TF_STATUS];
333 volatile uint16_t *task_file;
334 volatile uint16_t dummy;
336 task_file = (volatile uint16_t *) base_addr;
337 for (count = 0; count < SECTOR_SIZE; count+=2) {
338 uint16_t temp = *ptr_16++;
339 task_file[TF_DATA] = SWAP_SHORT(temp);
340 if ((count & 0xf) == 0) dummy = task_file[TF_STATUS/2];
346 #ifdef OCTEON_VISUAL_CF_1
347 octeon_led_write_char(1, ' ');
353 /* ------------------------------------------------------------------- *
354 * cf_cmd_identify() *
355 * ------------------------------------------------------------------- *
357 * Read parameters and other information from the drive and store
358 * it in the drive_param structure
361 static int cf_cmd_identify (void)
367 if (bus_width == 8) {
368 volatile uint8_t *task_file;
370 task_file = (volatile uint8_t *) base_addr;
372 while ((status = task_file[TF_STATUS]) & STATUS_BSY) {
376 task_file[TF_SECTOR_COUNT] = 0;
377 task_file[TF_SECTOR_NUMBER] = 0;
378 task_file[TF_CYL_LSB] = 0;
379 task_file[TF_CYL_MSB] = 0;
380 task_file[TF_DRV_HEAD] = 0;
381 task_file[TF_COMMAND] = CMD_IDENTIFY;
383 error = cf_wait_busy();
385 for (count = 0; count < SECTOR_SIZE; count++)
386 drive_param.u.buf[count] = task_file[TF_DATA];
389 volatile uint16_t *task_file;
391 task_file = (volatile uint16_t *) base_addr;
393 while ((status = (task_file[TF_STATUS/2]>>8)) & STATUS_BSY) {
397 task_file[TF_SECTOR_COUNT/2] = 0; /* this includes TF_SECTOR_NUMBER */
398 task_file[TF_CYL_LSB/2] = 0; /* this includes TF_CYL_MSB */
399 task_file[TF_DRV_HEAD/2] = 0 | (CMD_IDENTIFY<<8); /* this includes TF_COMMAND */
401 error = cf_wait_busy();
403 for (count = 0; count < SECTOR_SIZE; count+=2) {
405 temp = task_file[TF_DATA];
407 /* endianess will be swapped below */
408 drive_param.u.buf[count] = (temp & 0xff);
409 drive_param.u.buf[count+1] = (temp & 0xff00)>>8;
414 printf("%s: identify failed: %d\n", __func__, error);
418 cf_swap_ascii(drive_param.u.driveid.model, drive_param.model);
420 drive_param.sector_size = 512; //= SWAP_SHORT (drive_param.u.driveid.sector_bytes);
421 drive_param.heads = SWAP_SHORT (drive_param.u.driveid.current_heads);
422 drive_param.tracks = SWAP_SHORT (drive_param.u.driveid.current_cylinders);
423 drive_param.sec_track = SWAP_SHORT (drive_param.u.driveid.current_sectors);
424 drive_param.nr_sectors = (uint32_t)SWAP_SHORT (drive_param.u.driveid.lba_size_1) |
425 ((uint32_t)SWAP_SHORT (drive_param.u.driveid.lba_size_2));
431 /* ------------------------------------------------------------------- *
433 * ------------------------------------------------------------------- *
435 * Send command to read/write one sector specified by lba.
438 static int cf_send_cmd (uint32_t lba, uint8_t cmd)
442 if (bus_width == 8) {
443 volatile uint8_t *task_file;
445 task_file = (volatile uint8_t *) base_addr;
447 while ( (status = task_file[TF_STATUS]) & STATUS_BSY) {
451 task_file[TF_SECTOR_COUNT] = 1;
452 task_file[TF_SECTOR_NUMBER] = (lba & 0xff);
453 task_file[TF_CYL_LSB] = ((lba >> 8) & 0xff);
454 task_file[TF_CYL_MSB] = ((lba >> 16) & 0xff);
455 task_file[TF_DRV_HEAD] = ((lba >> 24) & 0xff) | 0xe0;
456 task_file[TF_COMMAND] = cmd;
459 volatile uint16_t *task_file;
461 task_file = (volatile uint16_t *) base_addr;
463 while ( (status = (task_file[TF_STATUS/2]>>8)) & STATUS_BSY) {
467 task_file[TF_SECTOR_COUNT/2] = 1 | ((lba & 0xff) << 8);
468 task_file[TF_CYL_LSB/2] = ((lba >> 8) & 0xff) | (((lba >> 16) & 0xff) << 8);
469 task_file[TF_DRV_HEAD/2] = (((lba >> 24) & 0xff) | 0xe0) | (cmd << 8);
473 return (cf_wait_busy());
476 /* ------------------------------------------------------------------- *
478 * ------------------------------------------------------------------- *
480 * Wait until the drive finishes a given command and data is
481 * ready to be transferred. This is done by repeatedly checking
482 * the BSY bit of the status register. When the controller is ready for
483 * data transfer, it clears the BSY bit and sets the DRQ bit.
485 * If the DF bit is ever set, we return error.
487 * This code originally spun on DRQ. If that behavior turns out to be
488 * necessary, a flag can be added or this function can be called
489 * repeatedly as long as it is returning ENXIO.
491 static int cf_wait_busy (void)
495 //#define OCTEON_VISUAL_CF_2 1
496 #ifdef OCTEON_VISUAL_CF_2
497 static int where0 = 0;
499 octeon_led_run_wheel(&where0, 2);
502 if (bus_width == 8) {
503 volatile uint8_t *task_file;
504 task_file = (volatile uint8_t *)base_addr;
506 status = task_file[TF_STATUS];
507 while ((status & STATUS_BSY) == STATUS_BSY) {
508 if ((status & STATUS_DF) != 0) {
509 printf("%s: device fault (status=%x)\n", __func__, status);
513 status = task_file[TF_STATUS];
516 volatile uint16_t *task_file;
517 task_file = (volatile uint16_t *)base_addr;
519 status = task_file[TF_STATUS/2]>>8;
520 while ((status & STATUS_BSY) == STATUS_BSY) {
521 if ((status & STATUS_DF) != 0) {
522 printf("%s: device fault (status=%x)\n", __func__, status);
526 status = (uint8_t)(task_file[TF_STATUS/2]>>8);
529 if ((status & STATUS_DRQ) == 0) {
530 printf("%s: device not ready (status=%x)\n", __func__, status);
534 #ifdef OCTEON_VISUAL_CF_2
535 octeon_led_write_char(2, ' ');
540 /* ------------------------------------------------------------------- *
542 * ------------------------------------------------------------------- *
544 * The ascii string returned by the controller specifying
545 * the model of the drive is byte-swaped. This routine
546 * corrects the byte ordering.
549 static void cf_swap_ascii (unsigned char str1[], char str2[])
553 for(i = 0; i < MODEL_STR_SIZE; i++) {
559 /* ------------------------------------------------------------------- *
561 * ------------------------------------------------------------------- */
563 static int cf_probe (device_t dev)
565 if (octeon_is_simulation()) return 1;
567 if (device_get_unit(dev) != 0) {
568 panic("can't attach more devices\n");
571 device_set_desc(dev, "Octeon Compact Flash Driver");
573 return (cf_cmd_identify());
576 /* ------------------------------------------------------------------- *
578 * ------------------------------------------------------------------- *
580 * Find the bootbus region for the CF to determine
581 * 16 or 8 bit and check to see if device is
585 static void cf_identify (driver_t *drv, device_t parent)
590 cvmx_mio_boot_reg_cfgx_t cfg;
592 if (octeon_is_simulation())
595 base_addr = cvmx_phys_to_ptr(octeon_bootinfo->compact_flash_common_base_addr);
597 for (bus_region = 0; bus_region < 8; bus_region++)
599 cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(bus_region));
600 if (cfg.s.base == octeon_bootinfo->compact_flash_common_base_addr >> 16)
602 bus_width = (cfg.s.width) ? 16: 8;
603 printf("Compact flash found in bootbus region %d (%d bit).\n", bus_region, bus_width);
608 if (bus_width == 8) {
609 volatile uint8_t *task_file;
610 task_file = (volatile uint8_t *) base_addr;
611 /* Check if CF is inserted */
612 while ( (status = task_file[TF_STATUS]) & STATUS_BSY){
613 if ((count++) == NR_TRIES ) {
614 printf("Compact Flash not present\n");
620 volatile uint16_t *task_file;
621 task_file = (volatile uint16_t *) base_addr;
622 /* Check if CF is inserted */
623 while ( (status = (task_file[TF_STATUS/2]>>8)) & STATUS_BSY){
624 if ((count++) == NR_TRIES ) {
625 printf("Compact Flash not present\n");
632 BUS_ADD_CHILD(parent, 0, "cf", 0);
636 /* ------------------------------------------------------------------- *
638 * ------------------------------------------------------------------- */
640 static int cf_attach_geom (void *arg, int flag)
642 struct cf_priv *cf_priv;
644 cf_priv = (struct cf_priv *) arg;
645 cf_priv->cf_geom = g_new_geomf(&g_cf_class, "cf%d", device_get_unit(cf_priv->dev));
646 cf_priv->cf_provider = g_new_providerf(cf_priv->cf_geom, cf_priv->cf_geom->name);
647 cf_priv->cf_geom->softc = cf_priv;
648 g_error_provider(cf_priv->cf_provider, 0);
653 /* ------------------------------------------------------------------- *
655 * ------------------------------------------------------------------- */
656 static void cf_attach_geom_proxy (void *arg, int flag)
658 cf_attach_geom(arg, flag);
663 /* ------------------------------------------------------------------- *
665 * ------------------------------------------------------------------- */
667 static int cf_attach (device_t dev)
669 struct cf_priv *cf_priv;
671 if (octeon_is_simulation()) return 1;
673 cf_priv = device_get_softc(dev);
675 cf_priv->drive_param = &drive_param;
677 g_post_event(cf_attach_geom_proxy, cf_priv, M_WAITOK, NULL);
678 bioq_init(&cf_priv->cf_bq);
684 static device_method_t cf_methods[] = {
685 /* Device interface */
686 DEVMETHOD(device_probe, cf_probe),
687 DEVMETHOD(device_identify, cf_identify),
688 DEVMETHOD(device_attach, cf_attach),
689 DEVMETHOD(device_detach, bus_generic_detach),
690 DEVMETHOD(device_shutdown, bus_generic_shutdown),
695 static driver_t cf_driver = {
698 sizeof(struct cf_priv)
701 static devclass_t cf_devclass;
703 DRIVER_MODULE(cf, nexus, cf_driver, cf_devclass, 0, 0);