gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/sysdsk/src/sdisk.hbare sourcepermlink (0.04 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: 
   15: 
   16: /*
   17:  *      sdisk.h              system disk driver
   18:  *
   19:  *      common definition
   20:  */
   21: #include <tk/tkernel.h>
   22: #include <libstr.h>
   23: #include <tk/util.h>
   24: #include <device/sdrvif.h>
   25: #include <device/gdrvif.h>
   26: #include <device/disk.h>
   27: #include <device/pcmcia.h>
   28: #include <sys/util.h>
   29: 
   30: /* debug message (disabled) */
   31: #define DP(arg)         /**/
   32: 
   33: /* Macro for 4 characters object creation   */
   34: #if     BIGENDIAN
   35: #define CH4toW(c1, c2, c3, c4)  ( ((c1)<<24)|((c2)<<16)|((c3)<<8)|(c4) )
   36: #else
   37: #define CH4toW(c1, c2, c3, c4)  ( ((c4)<<24)|((c3)<<16)|((c2)<<8)|(c1) )
   38: #endif
   39: 
   40: /*
   41:         Target disk information
   42: */
   43: typedef struct {
   44:         UW     accif:8;            /* Access interface       */
   45:         UW     blocksz:2;          /* Block size x 512     */
   46:         UW     rsv1:6;
   47:         BOOL   pccard:1;         /* PCMCIA card         */
   48:         BOOL   rsv2:1;
   49:         BOOL   readonly:1;               /* Readout only              */
   50:         BOOL   subtsk:1;         /* Necessity of subtask (unused)       */
   51:         BOOL   eject:1;          /* Availability of the eject            */
   52:         BOOL   lockreq:1;                /* Necessity of the lock (use DMA)    */
   53:         BOOL   wlock:1;          /* (Forced) write-protect               */
   54:         BOOL   susres:1;         /* here is power-supply control                */
   55:         BOOL   nowprotect:1;             /* No write-protect        */
   56:         UW     rsv3:7;
   57: } SDSpec;
   58: 
   59: #define MEM_ACCIF       1             /* Memory interface        */
   60: #define ATA_ACCIF       2             /* ATA/ ATAPI interface    */
   61: #define MMC_ACCIF       3             /* MMC/SD interface */
   62: 
   63:                         /* acc           bs rs pc us ro st ej lc wl sr nw rs */
   64: #define ROM_DISK        {MEM_ACCIF, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0}
   65: #define RAM_DISK        {MEM_ACCIF, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
   66: #define ATA_CARD        {ATA_ACCIF, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0}
   67: #define MMC_CARD        {MMC_ACCIF, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0}
   68: 
   69: #define MEM_DRV         0x00           /* Memory disk drive number      */
   70: #define PC_DRV          0x0C            /* PC card drive number   */
   71: 
   72: typedef struct {
   73:         W      num;                 /* Top drive number (0:next)  */
   74:         SDSpec spec;                   /* Device specification         */
   75:         UB     devnm[L_DEVNM];     /* Device name                     */
   76:         UW     name4;                      /* Object name (4 characters)      */
   77:         UW     drvno;                      /* Drive number                    */
   78:         UW     iob;                        /* IO base address           */
   79:         UW     misc;                       /* Various parameters               */
   80: } SDInfo;
   81: 
   82: /*                              iob                       misc
   83:  *      PC card              IO base address     --
   84:  *      memory disk          memory top address      memory size
   85:  */
   86: 
   87: /*
   88:  *      System disk drive information
   89:  */
   90: #define MAX_PART        4              /* Maximum number of partitions             */
   91: #define MAX_SUNIT       (MAX_PART + 1)        /* Maximum number of subunits          */
   92: #define IDENT_DTSZ      96           /* Identification information size       */
   93: #define IDENT2_DTSZ     16          /* Identification information (2) size  */
   94: 
   95: typedef struct {                        /* Subunit information               */
   96:         DiskSystemId SystemId;         /* System ID           */
   97:         UW     StartBlock;         /* Partition start block number        */
   98:         UW     EndBlock;           /* Partition end block number + 1        */
   99:         W      OpenCnt;             /* Open count              */
  100: } SUNIT;
  101: 
  102: typedef struct _drvtab {
  103:         /* Configuration information    */
  104:         SDSpec Spec;                   /* Disk specification           */
  105:         UB     DevName[L_DEVNM];   /* Device name                    */
  106:         UW     DrvNo;                      /* Drive number            */
  107:         UW     DrvBit;                     /* Drive number bit               */
  108:         UW     IOB;                        /* IO / memory base address */
  109:         UW     MiscPar;            /* Interrupt vector/memory size */
  110: 
  111:         struct _drvtab *Next;          /* Next drive                   */
  112:         struct _drvtab *Top;           /* Top drive             */
  113: 
  114:         /* Half-fixed setting information */
  115:         ID     MbfId;                      /* Message buffer for event notification */
  116:         GDI    Gdi;                       /* Driver I/F handler       */
  117:         ID     DevId;                      /* Device ID               */
  118:         ID     WaitTskId;          /* Wait-for-interrupt ID        */
  119: 
  120:         /* Processing function in accordance with device type */
  121:         void   (*Abort)(struct _drvtab *drv);
  122:         INT    (*ReadWrite)(struct _drvtab *drv, W blk, W cnt, void *mptr,
  123:                                                                 BOOL write);
  124:         ER     (*Format)(struct _drvtab *drv, DiskFormat *fmt, W dcnt);
  125:         ER     (*Identify)(struct _drvtab *drv, BOOL check);
  126:         ER     (*DiskInit)(struct _drvtab *drv);
  127:         ER     (*Misc)(struct _drvtab *drv, W cmd);
  128: 
  129:         void   (*IntHdr)(struct _drvtab *drv);
  130: 
  131:         /* Common flag */
  132:         BOOL   DriveOK:1;                /* Absence or presence of drive (card)        */
  133:         BOOL   MediaOK:1;                /* Absence or presence of media               */
  134:         BOOL   Wprotect:1;               /* Write-protection media            */
  135:         BOOL   EnbInt:1;         /* Interrupt-enabled (valid)           */
  136:         BOOL   Suspended:1;              /* During "suspend"                 */
  137:         BOOL   Aborted:1;                /* Abort request                      */
  138:         BOOL   Reset:1;          /* Reset flag           */
  139:         BOOL   SuptMBR:1;                /* "DISKMBR" support          */
  140: 
  141:         /* ATA I/F exclusive flag */
  142:         BOOL   UseLBA:1;         /* Use LBA             */
  143:         BOOL   SetXCHS:1;                /* XCHS data is already set   */
  144: 
  145:         BOOL   ResvFlags:22;             /* Reserve                         */
  146: 
  147:         /* Information for internal processing */
  148:         FastLock ProcLock;             /* Lock of common processing               */
  149:         T_DEVREQ *CurReq;              /* Request the abort target         */
  150:         TMO    ReqTmout;          /* Wait-for-request time-out            */
  151: 
  152:         /* PCMCIA card-related information */
  153:         union {
  154:                 struct {
  155:                         ID   CardId;           /* Card ID               */
  156:                         ID   MapId;            /* Map ID         */
  157:                         ID   MapId2;           /* Map ID 2      */
  158:                         W    CardInf;   /* Individual information of card */
  159:                         UH   IOConf[2];        /* IO configuration information        */
  160:                 } pcc;
  161:                 union {
  162:                         UW   uw[5];            /* General-purpose parameter      */
  163:                         UH   uh[10];
  164:                         UB   ub[20];
  165:                 } g;
  166:         } d;
  167: 
  168:         /* Disk information    */
  169:         DiskFormat DiskFmt;            /* Disk format            */
  170:         W      SecSize;             /* Disk sector size        */
  171:         W      SecBias;             /* Sector bias             */
  172: 
  173:         W      CurSUnit;            /* Subunit in processing  */
  174:         W      OpenCnt;             /* Open count              */
  175: 
  176:         UH     MultiCnt;           /* Multi count           */
  177:         UH     nSUnit;                     /* The number of valid subunits           */
  178: 
  179:         UH     nCyl;                       /* The number of cylinders for internal             */
  180:         UH     nHead;                      /* The number of heads for internal + other information    */
  181:         UH     nSec;                       /* The number of sectors for internal /Track        */
  182:         UH     nXCyl;                      /* The number of cylinders                 */
  183:         UH     nXHead;                     /* The number of heads                    */
  184:         UH     nXSec;                      /* The number of sectors/tracks            */
  185: 
  186:         union {                                /* Subunit information              */
  187:                 SUNIT SUnit[MAX_SUNIT];       /* [0] is the physical unit   */
  188:         } s;
  189:         UB     Ident[IDENT_DTSZ];  /* Identification information                    */
  190:         UB     Ident2[IDENT2_DTSZ];        /* Identification information (2)              */
  191: 
  192:         union {                                /* Individual working area          */
  193:                 UW    uw[1];
  194:                 UH    uh[2];
  195:                 UB    ub[4];
  196:         } wrk;
  197: } DrvTab;
  198: 
  199: #define ENB_INT(drv)    ((drv)->EnbInt = TRUE)             /* Enable interrupt*/
  200: #define DIS_INT(drv)    ((drv)->EnbInt = FALSE)            /* Disable interrupt*/
  201: 
  202: /* Command in "Misc" processing*/
  203: #define DC_OPEN         (1)
  204: #define DC_CLOSE        (2)
  205: #define DC_CLOSEALL     (3)
  206: #define DC_SUSPEND      (4)          /* "Spec.susres = 1" only       */
  207: #define DC_RESUME       (5)           /* "Spec.susres = 1" only        */
  208: #define DC_TSKINIT      (-1)         /* Task initialization processing              */
  209: #define DC_TSKTMO       (-2)          /* Task time-out processing     */
  210: #define DC_DRVCHG       (-3)          /* Drive change processing              */
  211: 
  212: /*
  213:  * Event time-out (msec)
  214:  */
  215: #define TMO_EVENT       (2 * 1000)    /* Event time-out          */
  216: 
  217: /*
  218:  *      Card power-supply OFF
  219:  */
  220: #define CP_POFF         CP_POFFREQ(5)  /* Card power supply is OFF in 5 seconds */
  221: 
  222: /*
  223:  *      Error code
  224:  */
  225: #define ERR_ABORT               (E_IO | 0)
  226: #define ERR_TMOUT               (E_IO | 1)
  227: #define ERR_MEDIA               (E_IO | 2)
  228: #define ERR_HARD                (E_IO | 3)
  229: #define ERR_DMA                 (E_IO | 4)
  230: #define ERR_BLKSZ               (E_NOSPT)
  231: 
  232: #define ERR_CMDBUSY             (E_IO | 0x10)
  233: #define ERR_DATABUSY            (E_IO | 0x11)
  234: #define ERR_NOTRDY              (E_IO | 0x12)
  235: 
  236: #define ERR_ATA(c)              (E_IO | 0x8000 | (c))
  237: #define ERR_ATAMSK(er, m)       ((er) & (0xffff8000 | (m)))
  238: 
  239: #define ERR_ATASENSE            ERR_ATA(0xF0)
  240: 
  241: #define ERR_NOBLK               (E_OBJ | 0)
  242: #define ERR_NOPORT              (E_OBJ | 1)
  243: #define ERR_NOMEDIA             E_NOMDA
  244: #define ERR_RONLY               E_RONLY
  245: #define ERR_NOTSAME             E_PAR
  246: 
  247: /*
  248:  *      Device request accept pattern
  249:  */
  250: #define DISWAI          16
  251: #define DISWAI_REQ      DEVREQ_ACPPTN(DISWAI)
  252: #define NORMAL_REQPTN   (DRP_NORMREQ | DISWAI_REQ)
  253: 
  254: /*
  255:  *      Invalid device ID
  256:  */
  257: #define INVALID_DEVID   (-1)
  258: 
  259: /*
  260:  *      Driver task information
  261:  */
  262: #ifdef  DEBUG
  263: #define PRINTF_STKSZ    (1536)
  264: #else
  265: #define PRINTF_STKSZ    (0)
  266: #endif
  267: #define DRVTSK_STKSZ    (3072 + PRINTF_STKSZ)      /* Stack size        */
  268: 
  269: /*
  270:  *      Function definition
  271:  */
  272: /* main.c */
  273: IMPORT  ER       SysDiskDrv(INT ac, UB *av[]);
  274: 
  275: /* accept.c */
  276: IMPORT  INT      CheckSize(W datacnt, W size);
  277: IMPORT  ER       sdAbortFn(T_DEVREQ *req, GDI gdi);
  278: IMPORT  INT      sdEventFn(INT evttyp, void *evtinf, GDI gdi);
  279: IMPORT  ER       sdOpenFn(ID devid, UINT omode, GDI gdi);
  280: IMPORT  ER       sdCloseFn(ID devid, UINT option, GDI gdi);
  281: IMPORT  void     sdAcceptRequest(DrvTab *drv);
  282: 
  283: /* common.c */
  284: IMPORT  void     sdSendEvt(DrvTab *drv, W kind);
  285: IMPORT  void     sdSetUpAccFn(DrvTab *drv);
  286: IMPORT  void     sdMakeDiskInfo(DrvTab *drv, DiskInfo *inf);
  287: IMPORT  ER       sdRegistDevice(DrvTab *drv);
  288: IMPORT  UW       sdGetMsec(void);
  289: IMPORT  BOOL     sdChkTmout(UW *tm, UW tmo, W delay);
  290: 
  291: /* inthdr.c */
  292: IMPORT  ER       sdDefIntHdr(DrvTab *drv, BOOL regist);
  293: IMPORT  void     sdSetIntWait(DrvTab *drv);
  294: 
  295: /* pccard.c */
  296: IMPORT  ER       sdPowerOn(DrvTab *drv, UW req);
  297: IMPORT  void     sdPowerOff(DrvTab *drv, UW req);
  298: IMPORT  INT      sdCardEvent(INT evttyp, CardReq *req, GDI gdi);
  299: IMPORT  ER       sdInitCard(DrvTab *drv);
  300: 
  301: /* ata.c */
  302: IMPORT  INT      ataReadWrite(DrvTab *drv, W blk, W cnt, void *mptr, BOOL write);
  303: IMPORT  ER       ataFormat(DrvTab *drv, DiskFormat *fmt, W dcnt);
  304: IMPORT  ER       ataIdentify(DrvTab *drv, BOOL check);
  305: IMPORT  ER       ataDiskInit(DrvTab *drv);
  306: IMPORT  ER       ataMisc(DrvTab *drv, W cmd);
  307: IMPORT  W        ataSetXCHS(DrvTab *drv);
  308: IMPORT  ER       ataSetupPart(DrvTab *drv);
  309: 
  310: /* atacmd.c */
  311: IMPORT  void     ataSelDrv(DrvTab *drv);
  312: IMPORT  void     ataIntHdr(DrvTab *drv);
  313: IMPORT  void     ataAbort(DrvTab *drv);
  314: IMPORT  ER       ataCmd(DrvTab *drv, W cmd, UW lba, W len, void *buf);
  315: IMPORT  INT      atapiCmd(DrvTab *drv, W cmd, UW lba, W len, void *buf);
  316: 
  317: /* mem.c */
  318: IMPORT  void     memAbort(DrvTab *drv);
  319: IMPORT  INT      memReadWrite(DrvTab *drv, W blk, W cnt, void *mptr, BOOL write);
  320: IMPORT  ER       memFormat(DrvTab *drv, DiskFormat *fmt, W dcnt);
  321: IMPORT  ER       memIdentify(DrvTab *drv, BOOL check);
  322: IMPORT  ER       memDiskInit(DrvTab *drv);
  323: IMPORT  ER       memMisc(DrvTab *drv, W cmd);
  324: 
  325: /* mmc-common.c */
  326: IMPORT  void     mmcAbort(DrvTab *drv);
  327: IMPORT  INT      mmcReadWrite(DrvTab *drv, W blk, W cnt, void *mptr, BOOL write);
  328: IMPORT  ER       mmcFormat(DrvTab *drv, DiskFormat *fmt, W dcnt);
  329: IMPORT  ER       mmcIdentify(DrvTab *drv, BOOL check);
  330: IMPORT  ER       mmcDiskInit(DrvTab *drv);
  331: IMPORT  ER       mmcMisc(DrvTab *drv, W cmd);
  332: 
  333: /* ataio.c */
  334: IMPORT  W        ataStatusIn(DrvTab *drv);
  335: IMPORT  W        ataErrorIn(DrvTab *drv);
  336: IMPORT  W        ataCylIn(DrvTab *drv);
  337: IMPORT  ER       ataSetDrive(DrvTab *drv, W drvno);
  338: IMPORT  void     ataEnbInt(DrvTab *drv);
  339: IMPORT  ER       ataWaitData(DrvTab *drv);
  340: IMPORT  ER       ataWaitReady(DrvTab *drv);
  341: IMPORT  void     ataReset(DrvTab *drv);
  342: IMPORT  void     atapiReset(DrvTab *drv);
  343: IMPORT  BOOL     atapiTest(DrvTab *drv, W init);
  344: IMPORT  ER       ataCommandOut(DrvTab *drv, W cmd, W cnt, W sec, W cyl, W head);
  345: IMPORT  ER       atapiCommandOut(DrvTab *drv, UH *cmd, W cnt, W dma);
  346: IMPORT  void     ataDataIn(DrvTab *drv, UH *buf, W cnt);
  347: IMPORT  void     ataDataSkip(DrvTab *drv, W cnt);
  348: IMPORT  void     ataDataOut(DrvTab *drv, UH *buf, W cnt);