Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

490 lines
16 KiB

  1. /*****************************************************************************
  2. * *
  3. * FAT-FTL Lite Software Development Kit *
  4. * Copyright (C) M-Systems Ltd. 1995-2001 *
  5. * *
  6. *****************************************************************************
  7. * *
  8. * In order to make your code faster: *
  9. * *
  10. * 1. Get rid of routines flRead8bitRegPlus()/flPreInitRead8bitReg()Plus, *
  11. * and make M+ MTD calling routine mplusReadReg8() directly. *
  12. * *
  13. * 2. Get rid of routines flWrite8bitRegPlus()/flPreInitWrite8bitReg()Plus, *
  14. * and make M+ MTD calling routine mplusWriteReg8() directly. *
  15. * *
  16. * 3. Eliminate overhead of calling routines tffscpy/tffsset() by *
  17. * adding these routines' code into docPlusRead/docPlusWrite/docPlusSet *
  18. * routines. *
  19. * *
  20. *****************************************************************************/
  21. /*
  22. * $Log: V:/Flite/archives/TrueFFS5/Src/docsysp.c_V $
  23. *
  24. * Rev 1.4 Nov 18 2001 20:26:50 oris
  25. * Bug fix- Bad implementation of 8-bit access when ENVIRNOMENT_VARS is not defined.
  26. *
  27. * Rev 1.3 Nov 16 2001 00:26:54 oris
  28. * When ENVIRONMENT_VARS is not defined use for loop of bytes instead of tffscpy.
  29. *
  30. * Rev 1.2 Sep 25 2001 15:39:38 oris
  31. * Bug fix - Add special support for flUse8Bit environement variable.
  32. *
  33. * Rev 1.1 Sep 24 2001 18:23:32 oris
  34. * Completely revised to support runtime true 16-bit access.
  35. */
  36. /*
  37. * configuration
  38. */
  39. /* #define FL_INIT_MMU_PAGES */
  40. /*
  41. * includes
  42. */
  43. #include "docsysp.h"
  44. /*
  45. * macros
  46. */
  47. /* types of access to M+: 8 or 16-bit */
  48. #define FL_MPLUS_ACCESS_8BIT 0x10
  49. #define FL_MPLUS_ACCESS_16BIT 0x20
  50. #define FL_MPLUS_ACCESS_MASK 0xf0 /* mask for the above */
  51. /* in case of 16-bit access to M+ */
  52. #define FL_MPLUS_ACCESS_BE 0x100
  53. /*
  54. * routines
  55. */
  56. #ifdef FL_INIT_MMU_PAGES
  57. static void flInitMMUpages (byte FAR1 *buf, int bufsize);
  58. #endif
  59. /*
  60. * vars
  61. */
  62. /* run-time configuration of DiskOnChip access */
  63. int flMplusAccessType = FL_MPLUS_ACCESS_8BIT;
  64. /* FL_MPLUS_ACCESS_8BIT
  65. FL_MPLUS_ACCESS_16BIT
  66. FL_MPLUS_ACCESS_BE */
  67. /* ---------------------------------------------------------------------- *
  68. * *
  69. * m p l u s R e a d R e g 8 *
  70. * *
  71. * Read single byte from memory mapped 8-bit M+ register. *
  72. * *
  73. * ---------------------------------------------------------------------- */
  74. unsigned char mplusReadReg8 ( void FAR0 * win, int offset )
  75. {
  76. if( flMplusAccessType & FL_MPLUS_ACCESS_16BIT ) {
  77. /* can't read byte, only short word */
  78. unsigned short sval;
  79. sval = *((volatile unsigned short FAR0 *)win + (offset >> 1));
  80. return *(((unsigned char *) &sval) + (offset & 0x1));
  81. }
  82. /* FL_MPLUS_ACCESS_8BIT case */
  83. return *((volatile unsigned char FAR0 *)win + offset);
  84. }
  85. /* ---------------------------------------------------------------------- *
  86. * *
  87. * m p l u s W r i t e R e g 8 *
  88. * *
  89. * Write single byte to memory mapped 8-bit M+ register. *
  90. * *
  91. * ---------------------------------------------------------------------- */
  92. void mplusWriteReg8 ( void FAR0 * win, int offset, unsigned char val )
  93. {
  94. switch( flMplusAccessType & FL_MPLUS_ACCESS_MASK ) {
  95. case FL_MPLUS_ACCESS_16BIT:
  96. *((volatile unsigned short FAR0 *)win + (offset >> 1)) =
  97. (unsigned short)(val * 0x0101);
  98. break;
  99. default: /* FL_MPLUS_ACCESS_8BIT */
  100. *((volatile unsigned char FAR0 *)win + offset) = val;
  101. break;
  102. }
  103. }
  104. /* ---------------------------------------------------------------------- *
  105. * *
  106. * f l R e a d 1 6 b i t R e g P l u s *
  107. * *
  108. * Read single word from memory mapped 16-bit M+ register. *
  109. * *
  110. * ---------------------------------------------------------------------- */
  111. Reg16bitType flRead16bitRegPlus ( FLFlash vol, unsigned offset )
  112. {
  113. return (Reg16bitType)
  114. (*((volatile unsigned short FAR0 *)((char FAR0 *)NFDC21thisVars->win + (int)offset)));
  115. }
  116. /* ---------------------------------------------------------------------- *
  117. * *
  118. * f l W r i t e 1 6 b i t R e g P l u s *
  119. * *
  120. * Write single word to memory mapped 16-bit M+ register. *
  121. * *
  122. * ---------------------------------------------------------------------- */
  123. void flWrite16bitRegPlus ( FLFlash vol, unsigned offset, Reg16bitType val )
  124. {
  125. *((volatile unsigned short FAR0 *)((char FAR0 *)NFDC21thisVars->win + (int)offset)) =
  126. (unsigned short) val;
  127. }
  128. /* ---------------------------------------------------------------------- *
  129. * *
  130. * d o c P l u s R e a d *
  131. * *
  132. * This routine is called from M+ MTD to read data block from M+. *
  133. * *
  134. * ---------------------------------------------------------------------- */
  135. void docPlusRead ( FLFlash vol, unsigned offset, void FAR1 * dest, unsigned int count )
  136. {
  137. volatile unsigned short FAR0 * swin;
  138. register int i;
  139. register unsigned short tmp;
  140. if (count == 0)
  141. return;
  142. #ifdef FL_INIT_MMU_PAGES
  143. flInitMMUpages( (byte FAR1 *)dest, (int)count );
  144. #endif
  145. if ((vol.interleaving == 1) && (NFDC21thisVars->if_cfg == 16)) {
  146. /* rare case: 16-bit hardware interface AND interleave == 1 */
  147. for (i = 0; i < (int)count; i++) {
  148. *((unsigned char FAR1 *)dest + i) =
  149. mplusReadReg8 ((void FAR0 *)NFDC21thisVars->win, ((int)offset));
  150. }
  151. }
  152. else {
  153. switch( flMplusAccessType & FL_MPLUS_ACCESS_MASK ) {
  154. case FL_MPLUS_ACCESS_16BIT:
  155. swin = (volatile unsigned short FAR0 *)NFDC21thisVars->win + ((int)offset >> 1);
  156. if( pointerToPhysical(dest) & 0x1 ) {
  157. /* rare case: unaligned target buffer */
  158. if( flMplusAccessType & FL_MPLUS_ACCESS_BE ) { /* big endian */
  159. for (i = 0; i < (int)count; ) {
  160. tmp = *(swin + (i >> 1));
  161. *((unsigned char FAR1 *)dest + (i++)) = (unsigned char) (tmp >> 8);
  162. *((unsigned char FAR1 *)dest + (i++)) = (unsigned char) tmp;
  163. }
  164. }
  165. else { /* little endian */
  166. for (i = 0; i < (int)count; ) {
  167. tmp = *(swin + (i >> 1));
  168. *((unsigned char FAR1 *)dest + (i++)) = (unsigned char) tmp;
  169. *((unsigned char FAR1 *)dest + (i++)) = (unsigned char) (tmp >> 8);
  170. }
  171. }
  172. }
  173. else { /* mainstream case */
  174. #ifdef ENVIRONMENT_VARS
  175. if (flUse8Bit == 0) {
  176. tffscpy( dest, (void FAR0 *)((NDOC2window)NFDC21thisWin + offset), count );
  177. }
  178. else
  179. #endif /* ENVIRONMENT_VARS */
  180. { /* read in short words */ /* andrayk: do we need this ? */
  181. for (i = 0; i < ((int)count >> 1); i++)
  182. *((unsigned short FAR1 *)dest + i) = *(swin + i);
  183. }
  184. }
  185. break;
  186. default: /* FL_MPLUS_ACCESS_8BIT */
  187. #ifdef ENVIRONMENT_VARS
  188. tffscpy( dest, (void FAR0 *)((NDOC2window)NFDC21thisWin + offset), count );
  189. #else
  190. for (i = 0; i < (int)count; i++)
  191. ((byte FAR1 *)dest)[i] = *((NDOC2window)NFDC21thisWin + offset);
  192. #endif /* ENVIRONMENT_VARS */
  193. break;
  194. }
  195. }
  196. }
  197. /* ---------------------------------------------------------------------- *
  198. * *
  199. * d o c P l u s W r i t e *
  200. * *
  201. * This routine is called from M+ MTD to write data block to M+. *
  202. * *
  203. * ---------------------------------------------------------------------- */
  204. void docPlusWrite ( FLFlash vol, void FAR1 * src, unsigned int count )
  205. {
  206. volatile unsigned short FAR0 * swin;
  207. register int i;
  208. register unsigned short tmp;
  209. if (count == 0)
  210. return;
  211. if ((vol.interleaving == 1) && (NFDC21thisVars->if_cfg == 16)) {
  212. /* rare case: 16-bit hardware interface AND interleave == 1 */
  213. for (i = 0; i < (int)count; i++) {
  214. mplusWriteReg8( (void FAR0 *)NFDC21thisVars->win, ((int)NFDC21thisIO),
  215. *((unsigned char FAR1 *)src + i) );
  216. }
  217. }
  218. else {
  219. switch( flMplusAccessType & FL_MPLUS_ACCESS_MASK ) {
  220. case FL_MPLUS_ACCESS_16BIT:
  221. swin = (volatile unsigned short FAR0 *)NFDC21thisVars->win + ((int)NFDC21thisIO >> 1);
  222. if( pointerToPhysical(src) & 0x1 ) {
  223. /* rare case: unaligned source buffer */
  224. if( flMplusAccessType & FL_MPLUS_ACCESS_BE ) { /* big endian */
  225. for (i = 0; i < (int)count; ) {
  226. tmp = ((unsigned short) (*((unsigned char FAR1 *)src + (i++)))) << 8;
  227. tmp |= (*((unsigned char FAR1 *)src + (i++)));
  228. *(swin + (i >> 1)) = tmp;
  229. }
  230. }
  231. else {
  232. for (i = 0; i < (int)count; ) { /* little endian */
  233. tmp = (*((unsigned char FAR1 *)src + (i++)));
  234. tmp |= ((unsigned short) (*((unsigned char FAR1 *)src + (i++)))) << 8;
  235. *(swin + (i >> 1)) = tmp;
  236. }
  237. }
  238. }
  239. else { /* mainstream case */
  240. #ifdef ENVIRONMENT_VARS
  241. if (flUse8Bit == 0) {
  242. tffscpy( (void FAR0 *)((NDOC2window)NFDC21thisWin + NFDC21thisIO), src, count );
  243. }
  244. else
  245. #endif /* ENVIRONMENT_VARS */
  246. { /* write in short words */ /* andrayk: do we need this ? */
  247. for (i = 0; i < ((int)count >> 1); i++)
  248. *(swin + i) = *((unsigned short FAR1 *)src + i);
  249. }
  250. }
  251. break;
  252. default: /* FL_MPLUS_ACCESS_8BIT */
  253. #ifdef ENVIRONMENT_VARS
  254. tffscpy( (void FAR0 *)((NDOC2window)NFDC21thisWin + NFDC21thisIO), src, count );
  255. #else
  256. for (i = 0; i < (int)count; i++)
  257. *((NDOC2window)NFDC21thisWin + NFDC21thisIO) =
  258. ((byte FAR1 *)src)[i];
  259. #endif /* ENVIRONMENT_VARS */
  260. break;
  261. }
  262. }
  263. }
  264. /* ---------------------------------------------------------------------- *
  265. * *
  266. * d o c P l u s S e t *
  267. * *
  268. * This routine is called from M+ MTD to set data block on M+ to 'val'. *
  269. * *
  270. * ---------------------------------------------------------------------- */
  271. void docPlusSet ( FLFlash vol, unsigned int count, unsigned char val )
  272. {
  273. volatile unsigned short FAR0 * swin;
  274. register int i;
  275. register unsigned short sval;
  276. if (count == 0)
  277. return;
  278. if ((vol.interleaving == 1) && (NFDC21thisVars->if_cfg == 16)) {
  279. /* rare case: 16-bit hardware interface AND interleave == 1 */
  280. for (i = 0; i < (int)count; i++)
  281. mplusWriteReg8( (void FAR0 *)NFDC21thisVars->win, (int)NFDC21thisIO, val );
  282. }
  283. else { /* mainstream case */
  284. switch( flMplusAccessType & FL_MPLUS_ACCESS_MASK ) {
  285. case FL_MPLUS_ACCESS_16BIT:
  286. #ifdef ENVIRONMENT_VARS
  287. if (flUse8Bit == 0) {
  288. tffsset( (void FAR0 *)((NDOC2window)NFDC21thisWin + NFDC21thisIO), val, count );
  289. }
  290. else
  291. #endif /* ENVIRONMENT_VARS */
  292. { /* do short word access */ /* andrayk: do we need this ? */
  293. swin = (volatile unsigned short FAR0 *)NFDC21thisVars->win +
  294. ((int)NFDC21thisIO >> 1);
  295. sval = ((unsigned short)val << 8) | val;
  296. for (i = 0; i < ((int)count >> 1); i++)
  297. *(swin + i) = sval;
  298. }
  299. break;
  300. default: /* FL_MPLUS_ACCESS_8BIT */
  301. #ifdef ENVIRONMENT_VARS
  302. tffsset( (void FAR0 *)((NDOC2window)NFDC21thisWin + NFDC21thisIO), val, count );
  303. #else
  304. for (i = 0; i < (int)count; i++)
  305. *((NDOC2window)NFDC21thisWin + NFDC21thisIO) = val;
  306. #endif /* ENVIRONMENT_VARS */
  307. break;
  308. }
  309. }
  310. }
  311. /* ---------------------------------------------------------------------- *
  312. * *
  313. * m p l u s W i n S i z e *
  314. * *
  315. * This routine is called from M+ MTD to find out size of M+ window in *
  316. * bytes. *
  317. * *
  318. * ---------------------------------------------------------------------- */
  319. unsigned long mplusWinSize ( void )
  320. {
  321. return 0x2000L;
  322. }
  323. #ifdef FL_INIT_MMU_PAGES
  324. /* ---------------------------------------------------------------------- *
  325. * *
  326. * f l I n i t M M U p a g e s *
  327. * *
  328. * Initializes the first and last byte of the given buffer. *
  329. * When the user buffer resides on separated memory pages the read *
  330. * operation may cause a page fault. Some CPU's return from a page *
  331. * fault (after loading the new page) and reread the bytes that caused *
  332. * the page fault from the new loaded page. In order to prevent such a *
  333. * case the first and last bytes of the buffer are written *
  334. * *
  335. * ---------------------------------------------------------------------- */
  336. static void flInitMMUpages ( byte FAR1 *buf, int bufsize )
  337. {
  338. *buf = (byte)0;
  339. *( addToFarPointer(buf, (bufsize - 1)) ) = (byte)0;
  340. }
  341. #endif /* FL_INIT_MMU_PAGES */