AliM1543C_usb.cpp

Go to the documentation of this file.
00001 /* ES40 emulator.
00002  * Copyright (C) 2007-2008 by the ES40 Emulator Project
00003  *
00004  * WWW    : http://sourceforge.net/projects/es40
00005  * E-mail : camiel@camicom.com
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  * 
00021  * Although this is not required, the author would appreciate being notified of, 
00022  * and receiving any modifications you may make to the source code that might serve
00023  * the general public.
00024  */
00025 
00046 #include "StdAfx.h"
00047 #include "AliM1543C_usb.h"
00048 #include "System.h"
00049 
00050 u32 usb_cfg_data[64] = {
00051   /*00*/  0x523710b9, // CFID: vendor + device
00052   /*04*/  0x02800000, // CFCS: command + status
00053   /*08*/  0x0c031003, // CFRV: class + revision
00054   /*0c*/  0x00000000, // CFLT: latency timer + cache line size
00055   /*10*/  0x00000000, // BAR0:
00056   /*14*/  0x00000000, // BAR1:
00057   /*18*/  0x00000000, // BAR2:
00058   /*1c*/  0x00000000, // BAR3:
00059   /*20*/  0x00000000, // BAR4:
00060   /*24*/  0x00000000, // BAR5:
00061   /*28*/  0x00000000, // CCIC: CardBus
00062   /*2c*/  0x00000000, // CSID: subsystem + vendor
00063   /*30*/  0x00000000, // BAR6: expansion rom base
00064   /*34*/  0x00000000, // CCAP: capabilities pointer
00065   /*38*/  0x00000000,
00066   /*3c*/  0x500001ff, // CFIT: interrupt configuration
00067   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00068   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00069   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
00070 };
00071 
00072 u32 usb_cfg_mask[64] = {
00073   /*00*/  0x00000000, // CFID: vendor + device
00074   /*04*/  0x00000157, // CFCS: command + status
00075   /*08*/  0x00000000, // CFRV: class + revision
00076   /*0c*/  0x0000ffff, // CFLT: latency timer + cache line size
00077   /*10*/  0xfffff000, // BAR0
00078   /*14*/  0x00000000, // BAR1:
00079   /*18*/  0x00000000, // BAR2:
00080   /*1c*/  0x00000000, // BAR3:
00081   /*20*/  0x00000000, // BAR4:
00082   /*24*/  0x00000000, // BAR5:
00083   /*28*/  0x00000000, // CCIC: CardBus
00084   /*2c*/  0x00000000, // CSID: subsystem + vendor
00085   /*30*/  0x00000000, // BAR6: expansion rom base
00086   /*34*/  0x00000000, // CCAP: capabilities pointer
00087   /*38*/  0x00000000,
00088   /*3c*/  0x000000ff, // CFIT: interrupt configuration
00089   /*40*/  0x04100000, // TM - test mode register
00090   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00091   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00092   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
00093 };
00094 
00098 CAliM1543C_usb::CAliM1543C_usb(CConfigurator*  cfg, CSystem*  c, int pcibus,
00099                                int pcidev) : CPCIDevice(cfg, c, pcibus, pcidev)
00100 {
00101   add_function(0, usb_cfg_data, usb_cfg_mask);
00102 
00103   ResetPCI();
00104 
00105   state.usb_data[0x34 / 4] = 0x2edf;
00106   state.usb_data[0x48 / 4] = 0x01000003;
00107 
00108   printf("%s: $Id: AliM1543C_usb.cpp,v 1.6 2008/03/14 15:30:50 iamcamiel Exp $\n",
00109          devid_string);
00110 }
00111 
00112 CAliM1543C_usb::~CAliM1543C_usb()
00113 { }
00114 u32 CAliM1543C_usb::ReadMem_Bar(int func, int bar, u32 address, int dsize)
00115 {
00116   u32 data = 0;
00117   switch(bar)
00118   {
00119   case 0:   data = usb_hci_read(address, dsize); break;
00120   default:  printf("%%USB-W-READBAR: Bad BAR %d selected.\n", bar);
00121   }
00122 
00123   return data;
00124 }
00125 
00126 void CAliM1543C_usb::WriteMem_Bar(int func, int bar, u32 address, int dsize,
00127                                   u32 data)
00128 {
00129   switch(bar)
00130   {
00131   case 0:   usb_hci_write(address, dsize, data); break;
00132   default:  printf("%%USB-W-WRITEBAR: Bad BAR %d selected.\n", bar);
00133   }
00134 
00135   return;
00136 }
00137 
00138 u64 CAliM1543C_usb::usb_hci_read(u64 address, int dsize)
00139 {
00140   u64 data = 0;
00141   if(dsize != 32)
00142     printf("%%USB-W-HCIREAD: Non dword read, returning 32 bits anyway.\n");
00143   switch(address)
00144   {
00145   case 0:     // HcRevision
00146     data = 0x00000110;
00147     break;
00148 
00149   case 4:     // HcControl
00150   case 8:     // HcCommandStatus
00151   case 0x0c:  // HcInterruptStatus
00152   case 0x10:  // HcInterrupt Enable
00153   case 0x14:  // HcInterruptDisable
00154   case 0x18:  // HcHCCA (datasheet says 0x17, but that's wrong)
00155   case 0x1c:  // HcPeriodCurrentED
00156   case 0x20:  // HcControlHeadED
00157   case 0x24:  // HcControlCurrentED
00158   case 0x28:  // HcBulkHeadED
00159   case 0x2c:  // HcBulkCurrentED
00160   case 0x30:  // HcDoneHead
00161   case 0x34:  // HcFmInterval
00162   case 0x38:  // HcFrameRemaining
00163   case 0x3c:  // HcFmNumber
00164   case 0x40:  // HcPeriodicStart
00165   case 0x44:  // HcLSThreshold
00166   case 0x48:  // HcRhDescriptorA
00167   case 0x4c:  // HcRhDescriptorB
00168   case 0x50:  // HcRhStatus
00169   case 0x54:  // HcRhPortStatus1
00170   case 0x58:  // HcRhPortStatus1
00171   case 0x5c:  // HcRhPortStatus1
00172   case 0x100: // HceControlRegister
00173   case 0x104: // HceInputRegister
00174   case 0x108: // HceOutputRegister
00175   case 0x10c: // HceStatusRegister
00176     data = state.usb_data[address / 4];
00177     break;
00178 
00179   default:
00180     printf("%%USB-W-HCIREAD: Reading from unknown address %x.  Ignoring.\n",
00181            (int) address);
00182   }
00183 
00184   return data;
00185 }
00186 
00187 void CAliM1543C_usb::usb_hci_write(u64 address, int dsize, u64 data)
00188 {
00189   if(dsize != 32)
00190     printf("%%USB-W-HCIWRITE: Non dword write, writing 32 bits anyway.\n");
00191   switch(address)
00192   {
00193   case 4:     // HcControl
00194   case 8:     // HcCommandStatus
00195   case 0x0c:  // HcInterruptStatus
00196   case 0x10:  // HcInterrupt Enable
00197   case 0x14:  // HcInterruptDisable
00198   case 0x18:  // HcHCCA (datasheet says 0x17, but that's wrong)
00199   case 0x1c:  // HcPeriodCurrentED
00200   case 0x20:  // HcControlHeadED
00201   case 0x24:  // HcControlCurrentED
00202   case 0x28:  // HcBulkHeadED
00203   case 0x2c:  // HcBulkCurrentED
00204   case 0x30:  // HcDoneHead
00205   case 0x34:  // HcFmInterval
00206   case 0x38:  // HcFrameRemaining
00207   case 0x3c:  // HcFmNumber
00208   case 0x40:  // HcPeriodicStart
00209   case 0x44:  // HcLSThreshold
00210   case 0x48:  // HcRhDescriptorA
00211   case 0x4c:  // HcRhDescriptorB
00212   case 0x50:  // HcRhStatus
00213   case 0x54:  // HcRhPortStatus1
00214   case 0x58:  // HcRhPortStatus1
00215   case 0x5c:  // HcRhPortStatus1
00216   case 0x100: // HceControlRegister
00217   case 0x104: // HceInputRegister
00218   case 0x108: // HceOutputRegister
00219   case 0x10c: // HceStatusRegister
00220     state.usb_data[address / 4] = data;
00221     break;
00222 
00223   default:
00224     printf("%%USB-W-HCIWRITE: Writing to unknown address %x.  Ignoring.\n",
00225            (int) address);
00226   }
00227 }
00228 
00229 static u32  usb_magic1 = 0x9000432B;
00230 static u32  usb_magic2 = 0xB2340009;
00231 
00235 int CAliM1543C_usb::SaveState(FILE* f)
00236 {
00237   long  ss = sizeof(state);
00238   int   res;
00239 
00240   if(res = CPCIDevice::SaveState(f))
00241     return res;
00242 
00243   fwrite(&usb_magic1, sizeof(u32), 1, f);
00244   fwrite(&ss, sizeof(long), 1, f);
00245   fwrite(&state, sizeof(state), 1, f);
00246   fwrite(&usb_magic2, sizeof(u32), 1, f);
00247   printf("%s: %d bytes saved.\n", devid_string, (int) ss);
00248   return 0;
00249 }
00250 
00254 int CAliM1543C_usb::RestoreState(FILE* f)
00255 {
00256   long    ss;
00257   u32     m1;
00258   u32     m2;
00259   int     res;
00260   size_t  r;
00261 
00262   if(res = CPCIDevice::RestoreState(f))
00263     return res;
00264 
00265   r = fread(&m1, sizeof(u32), 1, f);
00266   if(r != 1)
00267   {
00268     printf("%s: unexpected end of file!\n", devid_string);
00269     return -1;
00270   }
00271 
00272   if(m1 != usb_magic1)
00273   {
00274     printf("%s: MAGIC 1 does not match!\n", devid_string);
00275     return -1;
00276   }
00277 
00278   fread(&ss, sizeof(long), 1, f);
00279   if(r != 1)
00280   {
00281     printf("%s: unexpected end of file!\n", devid_string);
00282     return -1;
00283   }
00284 
00285   if(ss != sizeof(state))
00286   {
00287     printf("%s: STRUCT SIZE does not match!\n", devid_string);
00288     return -1;
00289   }
00290 
00291   fread(&state, sizeof(state), 1, f);
00292   if(r != 1)
00293   {
00294     printf("%s: unexpected end of file!\n", devid_string);
00295     return -1;
00296   }
00297 
00298   r = fread(&m2, sizeof(u32), 1, f);
00299   if(r != 1)
00300   {
00301     printf("%s: unexpected end of file!\n", devid_string);
00302     return -1;
00303   }
00304 
00305   if(m2 != usb_magic2)
00306   {
00307     printf("%s: MAGIC 1 does not match!\n", devid_string);
00308     return -1;
00309   }
00310 
00311   printf("%s: %d bytes restored.\n", devid_string, (int) ss);
00312   return 0;
00313 }

SourceForge.net Logo
Project space on SourceForge.net