ide.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  1. /****************************************************************************
  2. * This file is part of the AVRIDE device driver.
  3. *
  4. * Copyright (c) 2002-2003 by Michael Fischer. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the author nor the names of its contributors may
  16. * be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  23. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. ****************************************************************************
  33. * History:
  34. *
  35. * 14.12.02 mifi First Version
  36. * 29.12.02 mifi Support CF-Card too .
  37. * 01.01.03 mifi Add support for IDELock, IDEFree.
  38. * 08.01.03 mifi Now support several modes, like:
  39. * IDE_HARDDISK 16bit 14.7456Mhz / 2 wait states
  40. * IDE_HARDDISK_7MHZ 16bit 7.3728Mhz / 2 wait states
  41. * IDE_COMPACT_FLASH 16bit 14.7456Mhz / 2 wait states
  42. * MEM_8BIT_COMPACT_FLASH 8bit 14.7456Mhz / 2 wait states
  43. * 18.01.03 mifi Change Licence from GPL to BSD.
  44. * 25.01.03 mifi Fix the bug in ClearEvent.
  45. * Change IDEInit and add "AutoMount", "AutoUnMount"
  46. * callback function. With these functions we are
  47. * independent from the FileSystem.
  48. * Now support changing of the CF-Card in a running system.
  49. * 19.06.03 mifi Change IDEInit, if the BaseAddress is 0, we use the
  50. * default address IDE_BASE_ADDRESS. Now we can use the
  51. * address 0 in FAT.C and hide the ide stuff.
  52. * 24.06.03 mifi Enable the IDE Reset pin with DDRD = 0x20,
  53. * add some stuff to detect PACKET device.
  54. * 25.06.03 mifi Fix overflow with IDE_MAX_SUPPORTED_DEVICE
  55. * 29.06.03 mifi First ATAPI-Version
  56. ****************************************************************************/
  57. #define __IDE_C__
  58. #include <stddef.h>
  59. #include <string.h>
  60. #include <sys/timer.h>
  61. #include <sys/thread.h>
  62. #include <sys/event.h>
  63. #include <sys/atom.h>
  64. #include <sys/heap.h>
  65. #include <dev/irqreg.h>
  66. #include <dev/ide.h>
  67. #include <dev/idep.h>
  68. #include <fs/typedefs.h>
  69. #define HIBYTE(_x) (BYTE)((_x >> 8) & 0x00FF)
  70. #define LOBYTE(_x) (BYTE)(_x & 0x00FF)
  71. /*==========================================================*/
  72. /* DEFINE: All Structures and Common Constants */
  73. /*==========================================================*/
  74. //
  75. // Here are some important values.
  76. // Change these values if you change the hardware.
  77. //
  78. #define IDE_IRQ INT7
  79. #define IDE_INT_RISING_EDGE 0xC0
  80. #define CF_IRQ INT6
  81. #define CF_INT_SENS_MASK 0x30
  82. #define CF_INT_FALLING_EDGE 0x20
  83. #define CF_INT_RISING_EDGE 0x30
  84. /************************************************/
  85. #define IDE_MAX_SUPPORTED_DEVICE 1
  86. #define CF_AVAILABLE 0
  87. #define CF_NOT_AVAILABLE 1
  88. #define INITTIMEOUT 10000
  89. #define DISKTIMEOUT 10000
  90. #define CONTROLLERTIMEOUT 200
  91. #define ATAPITIMEOUT 5000
  92. #define IDEIn(x) pIDE[x]
  93. #define IDEOut(x,y) pIDE[x] = y
  94. //
  95. // Drive Flags
  96. //
  97. #define IDE_SUPPORT_LBA 0x0001
  98. #define IDE_SUPPORT_LBA48 0x0002
  99. #define IDE_SUPPORT_PACKET 0x0004
  100. //
  101. // Device shall assert INTRQ when DRQ
  102. // is set to one after receiving PACKET.
  103. //
  104. #define IDE_SUPPORT_INTRQ_PACKET 0x0008
  105. #define IDE_CDROM_DEVICE 0x1000
  106. #define IDE_ZIP_DEVICE 0x2000
  107. #define IDE_READ_ONLY 0x4000
  108. #define IDE_READY 0x8000
  109. //
  110. // Identify packet device
  111. // config word[0] bit masks
  112. //
  113. #define ATAPI_CFG_12_BYTE_MSK 0x0003
  114. #define ATAPI_CFG_INTRQ 0x0060
  115. #define ATAPI_CFG_REM_MEDIUM 0x0080
  116. #define ATAPI_CFG_DEVICE 0x1F00
  117. #define ATAPI_CFG_ATAPI 0xC000
  118. #define ATAPI_IS_12_BYTE 0x0000
  119. #define ATAPI_USE_INTRQ 0x0020
  120. #define ATAPI_IS_DIRECT_ACCESS 0x0000
  121. #define ATAPI_IS_CDROM 0x0500
  122. #define ATAPI_IS_PACKET 0x8000
  123. typedef struct _drive {
  124. //
  125. // Interface values
  126. //
  127. WORD wFlags;
  128. BYTE bIDEMode;
  129. BYTE bDevice;
  130. #if (IDE_SUPPORT_CHS == 1)
  131. //
  132. // CHS values
  133. //
  134. WORD wCylinders;
  135. WORD wHeads;
  136. WORD wSectorsPerTrack;
  137. DWORD dwOneSide;
  138. #endif
  139. //
  140. // LBA value
  141. //
  142. DWORD dwTotalSectors;
  143. WORD wSectorSize;
  144. } DRIVE, *LPDRIVE;
  145. /*==========================================================*/
  146. /* DEFINE: Prototypes */
  147. /*==========================================================*/
  148. /*==========================================================*/
  149. /* DEFINE: Definition of all local Data */
  150. /*==========================================================*/
  151. static HANDLE hIDEEvent;
  152. static volatile BYTE *pIDE;
  153. static volatile BYTE gbIntStatus = 0;
  154. static DRIVE sDrive[IDE_MAX_SUPPORTED_DEVICE];
  155. static HANDLE hIDESemaphore;
  156. static HANDLE hCFChangeInt;
  157. static BYTE gbCFMountStatus = 0;
  158. static IDE_MOUNT_FUNC *pUserMountFunc = NULL;
  159. static IDE_MOUNT_FUNC *pUserUnMountFunc = NULL;
  160. #if (IDE_SUPPORT_ATAPI == 1)
  161. //
  162. // ATAPI stuff
  163. //
  164. static BYTE aATAPICmd[12];
  165. #define ATAPI_CMD(_x) { \
  166. memset(aATAPICmd, 0x00, sizeof(aATAPICmd)); \
  167. aATAPICmd[0] = _x; \
  168. }
  169. #define CLEAR_ATAPI_CMD() memset(aATAPICmd, 0x00, sizeof(aATAPICmd));
  170. #endif
  171. /*==========================================================*/
  172. /* DEFINE: Definition of all local Procedures */
  173. /*==========================================================*/
  174. /************************************************************/
  175. /* Wait400ns */
  176. /************************************************************/
  177. static void Wait400ns(void)
  178. {
  179. //
  180. // Wait at least 400ns, with 14.7456Mhz we need
  181. // 8 nop's for 500ns
  182. //
  183. COMPRESS_DISABLE;
  184. _NOP();
  185. _NOP();
  186. _NOP();
  187. _NOP();
  188. _NOP();
  189. _NOP();
  190. _NOP();
  191. _NOP();
  192. COMPRESS_REENABLE;
  193. }
  194. /************************************************************/
  195. /* IDELock */
  196. /************************************************************/
  197. void IDELock(void)
  198. {
  199. NutEventWait(&hIDESemaphore, 0);
  200. }
  201. /************************************************************/
  202. /* IDEFree */
  203. /************************************************************/
  204. void IDEFree(void)
  205. {
  206. NutEventPost(&hIDESemaphore);
  207. }
  208. /************************************************************/
  209. /* IDESemaInit */
  210. /************************************************************/
  211. void IDESemaInit(void)
  212. {
  213. NutEventPost(&hIDESemaphore);
  214. }
  215. /************************************************************/
  216. /* HardwareReset */
  217. /************************************************************/
  218. static void HardwareReset(DRIVE * pDrive)
  219. {
  220. if (pDrive->bIDEMode == MEM_8BIT_COMPACT_FLASH) {
  221. //
  222. // Set Reset
  223. //
  224. PORTD |= 0x20;
  225. _NOP();
  226. NutSleep(1000);
  227. //
  228. // Remove Reset
  229. //
  230. PORTD &= ~0x20;
  231. _NOP();
  232. NutSleep(1000);
  233. } else { /* HD */
  234. //
  235. // Set RESET
  236. //
  237. PORTD &= ~0x20;
  238. _NOP();
  239. NutSleep(1000);
  240. //
  241. // Remove RESET
  242. //
  243. PORTD |= 0x20;
  244. _NOP();
  245. if (pDrive->bIDEMode == IDE_COMPACT_FLASH) {
  246. NutSleep(1000);
  247. } else {
  248. //
  249. // It can takes up to 10 seconds (2.5-Inch drive)
  250. // from reset high to NOT busy.
  251. // Take a look in the drive manual "Reset timings"
  252. //
  253. NutSleep(10000);
  254. }
  255. }
  256. }
  257. /************************************************************/
  258. /* CFInterrupt */
  259. /************************************************************/
  260. static void CFInterrupt(void *p)
  261. {
  262. p = p;
  263. NutEventPostFromIrq(&hCFChangeInt);
  264. }
  265. /************************************************************/
  266. /* IDEInterrupt */
  267. /************************************************************/
  268. static void IDEInterrupt(void *p)
  269. {
  270. p = p;
  271. gbIntStatus = IDEIn(STATUS_REG);
  272. NutEventPostFromIrq(&hIDEEvent);
  273. }
  274. /************************************************************/
  275. /* ClearEvent */
  276. /************************************************************/
  277. static void ClearEvent(HANDLE * pEvent)
  278. {
  279. NutEnterCritical();
  280. *pEvent = 0;
  281. NutExitCritical();
  282. }
  283. /************************************************************/
  284. /* WaitForInterrupt */
  285. /* */
  286. /* Return: IDE_OK, IDE_ERROR */
  287. /************************************************************/
  288. static int WaitForInterrupt(DWORD dwTimeout)
  289. {
  290. int nError;
  291. int nTimeout;
  292. nError = IDE_OK;
  293. nTimeout = NutEventWait(&hIDEEvent, dwTimeout);
  294. if (nTimeout == -1) {
  295. nError = IDE_ERROR;
  296. }
  297. return (nError);
  298. }
  299. #if ((IDE_SUPPORT_WRITE == 1) || (IDE_SUPPORT_ATAPI == 1))
  300. /************************************************************/
  301. /* WaitDRQ */
  302. /* */
  303. /* Return: IDE_OK, IDE_ERROR */
  304. /************************************************************/
  305. static int WaitDRQ(DWORD dwTimeout)
  306. {
  307. int nError;
  308. BYTE bStatus;
  309. nError = IDE_OK;
  310. bStatus = IDEIn(STATUS_REG);
  311. if ((bStatus & (STATUS_BUSY | STATUS_DATA_REQUEST)) != STATUS_DATA_REQUEST) {
  312. dwTimeout = (DWORD) (((dwTimeout * 10UL) / 625UL) + 1UL);
  313. dwTimeout += NutGetTickCount();
  314. bStatus = IDEIn(STATUS_REG);
  315. while ((bStatus & (STATUS_BUSY | STATUS_DATA_REQUEST)) != STATUS_DATA_REQUEST) {
  316. if (NutGetTickCount() > dwTimeout) {
  317. nError = IDE_ERROR;
  318. break;
  319. }
  320. bStatus = IDEIn(STATUS_REG);
  321. }
  322. }
  323. return (nError);
  324. }
  325. #endif
  326. /************************************************************/
  327. /* WaitNotBusy (see ATAPI-4 r17 p223 9.6) */
  328. /* */
  329. /* Return: IDE_OK, IDE_ERROR */
  330. /************************************************************/
  331. static int WaitNotBusy(DWORD dwTimeout)
  332. {
  333. int nError;
  334. BYTE bStatus;
  335. nError = IDE_OK;
  336. bStatus = IDEIn(STATUS_REG);
  337. if (bStatus & (STATUS_BUSY | STATUS_DATA_REQUEST)) {
  338. dwTimeout = ((dwTimeout * 10) / 625) + 1;
  339. dwTimeout += NutGetTickCount();
  340. bStatus = IDEIn(STATUS_REG);
  341. while (bStatus & (STATUS_BUSY | STATUS_DATA_REQUEST)) {
  342. if (NutGetTickCount() > dwTimeout) {
  343. nError = IDE_BUSY;
  344. break;
  345. }
  346. bStatus = IDEIn(STATUS_REG);
  347. }
  348. }
  349. return (nError);
  350. }
  351. /************************************************************/
  352. /* SelectDevice (see ATAPI-4 r17 p223 9.6) */
  353. /* */
  354. /* Return: IDE_OK, IDE_ERROR */
  355. /************************************************************/
  356. static int SelectDevice(BYTE bDevice)
  357. {
  358. int nError;
  359. int nTimeout;
  360. BYTE bStatus;
  361. BYTE bDummy;
  362. nError = IDE_OK;
  363. if (WaitNotBusy(CONTROLLERTIMEOUT) == IDE_OK) {
  364. IDEOut(DISK_HEAD_REG, 0xA0 + (bDevice << 4));
  365. Wait400ns();
  366. nError = WaitNotBusy(CONTROLLERTIMEOUT);
  367. } else {
  368. nError = IDE_BUSY;
  369. }
  370. if (nError != IDE_OK) {
  371. //
  372. // Test for special case, data available.
  373. // If data available, read the data!!
  374. //
  375. bStatus = IDEIn(STATUS_REG);
  376. if (bStatus & STATUS_DATA_REQUEST) {
  377. nTimeout = 512;
  378. while ((bStatus & STATUS_DATA_REQUEST) && (nTimeout)) {
  379. bDummy = IDEIn(DATA_READ_REG_LOW);
  380. bDummy = IDEIn(DATA_READ_REG_LOW);
  381. bStatus = IDEIn(STATUS_REG);
  382. nTimeout--;
  383. }
  384. }
  385. }
  386. return (nError);
  387. }
  388. #if (IDE_SUPPORT_ATAPI == 1)
  389. /************************************************************/
  390. /* ATAPISendCommand */
  391. /* */
  392. /* Return: IDE_OK, IDE_ERROR */
  393. /************************************************************/
  394. static int ATAPISendCommand(LPDRIVE pDrive, BYTE * pSectorBuffer, WORD * pReadCount)
  395. {
  396. int nError;
  397. BYTE bStatus;
  398. BYTE bDevice;
  399. WORD x, y;
  400. BYTE bDummy;
  401. WORD wSectorSize;
  402. nError = IDE_ERROR;
  403. bDummy = 0;
  404. bDevice = pDrive->bDevice;
  405. wSectorSize = pDrive->wSectorSize;
  406. *pReadCount = 0;
  407. if (SelectDevice(bDevice) == IDE_OK) {
  408. ClearEvent(&hIDEEvent);
  409. if (WaitNotBusy(ATAPITIMEOUT) == IDE_OK) {
  410. IDEOut(FEATURE_REG, 0x00);
  411. IDEOut(SECTOR_COUNT_REG, 0x00);
  412. IDEOut(SECTOR_REG, 0x00);
  413. IDEOut(CYLINDER_LOW_REG, LOBYTE(wSectorSize));
  414. IDEOut(CYLINDER_HIGH_REG, HIBYTE(wSectorSize));
  415. IDEOut(COMMAND_REG, 0xA0);
  416. if (pDrive->wFlags & IDE_SUPPORT_INTRQ_PACKET) {
  417. nError = WaitForInterrupt(ATAPITIMEOUT);
  418. if (nError == IDE_OK) {
  419. ClearEvent(&hIDEEvent);
  420. }
  421. } else {
  422. nError = WaitDRQ(ATAPITIMEOUT);
  423. }
  424. if (nError == IDE_OK) {
  425. for (x = 0; x < 12; x = x + 2) {
  426. IDEOut(DATA_WRITE_REG_HIGH, aATAPICmd[x + 1]);
  427. IDEOut(DATA_WRITE_REG_LOW, aATAPICmd[x]);
  428. }
  429. if (WaitForInterrupt(ATAPITIMEOUT) == IDE_OK) {
  430. if (gbIntStatus & STATUS_ERROR) {
  431. nError = IDE_ERROR;
  432. } else {
  433. nError = IDE_OK;
  434. if (gbIntStatus & STATUS_DATA_REQUEST) {
  435. Wait400ns();
  436. y = IDEIn(CYLINDER_HIGH_REG) << 8;
  437. y |= IDEIn(CYLINDER_LOW_REG);
  438. if (y > wSectorSize) {
  439. y = wSectorSize;
  440. }
  441. if (pSectorBuffer != NULL) {
  442. for (x = 0; x < y; x = x + 2) {
  443. pSectorBuffer[x] = pIDE[DATA_READ_REG_LOW];
  444. pSectorBuffer[x + 1] = pIDE[DATA_READ_REG_HIGH];
  445. }
  446. } else {
  447. for (x = 0; x < y; x = x + 2) {
  448. bDummy += pIDE[DATA_READ_REG_LOW];
  449. bDummy += pIDE[DATA_READ_REG_HIGH];
  450. }
  451. }
  452. *pReadCount = y;
  453. bStatus = IDEIn(STATUS_REG);
  454. } /* endif STATUS_DATA_REQUEST */
  455. } /* endif STATUS_ERROR */
  456. } /* endif WaitForInterrupt(ATAPITIMEOUT) */
  457. } /* endif (WaitDRQ(ATAPITIMEOUT) */
  458. } /* endif WaitNotBusy(ATAPITIMEOUT) */
  459. }
  460. /* endif SelectDevice(bDevice) */
  461. return (nError);
  462. }
  463. /************************************************************/
  464. /* ATAPIGetTotalSectors */
  465. /* */
  466. /* Return: IDE_OK, IDE_ERROR */
  467. /************************************************************/
  468. static int ATAPIGetTotalSectors(LPDRIVE pDrive, BYTE * pSectorBuffer, DWORD * pMaxSectors)
  469. {
  470. int nError;
  471. WORD wReadCount;
  472. WORD wErrorCount;
  473. DWORD dwValue;
  474. nError = IDE_OK;
  475. *pMaxSectors = 0;
  476. wErrorCount = 4;
  477. while (wErrorCount) {
  478. ATAPI_CMD(ATAPI_CMD_READ_CAPACITY);
  479. nError = ATAPISendCommand(pDrive, pSectorBuffer, &wReadCount);
  480. if ((nError == IDE_OK) && (wReadCount == 8)) {
  481. dwValue = pSectorBuffer[0];
  482. dwValue = dwValue << 8;
  483. dwValue |= pSectorBuffer[1];
  484. dwValue = dwValue << 8;
  485. dwValue |= pSectorBuffer[2];
  486. dwValue = dwValue << 8;
  487. dwValue |= pSectorBuffer[3];
  488. *pMaxSectors = dwValue;
  489. //
  490. // Get SectorSize
  491. //
  492. dwValue = pSectorBuffer[4];
  493. dwValue = dwValue << 8;
  494. dwValue |= pSectorBuffer[5];
  495. dwValue = dwValue << 8;
  496. dwValue |= pSectorBuffer[6];
  497. dwValue = dwValue << 8;
  498. dwValue |= pSectorBuffer[7];
  499. if (dwValue > ATAPI_SECTOR_SIZE) {
  500. dwValue = ATAPI_SECTOR_SIZE;
  501. }
  502. pDrive->wSectorSize = (WORD) (dwValue & 0x0000FFFF);
  503. break;
  504. }
  505. wErrorCount--;
  506. NutSleep(2000);
  507. }
  508. return (nError);
  509. }
  510. /************************************************************/
  511. /* GetDeviceInfoPacket */
  512. /* */
  513. /* PIO data in command protocol (see ATAPI-4 r17 p224 9.7) */
  514. /* */
  515. /* Return: IDE_OK, IDE_ERROR */
  516. /************************************************************/
  517. static int GetDeviceInfoPacket(LPDRIVE pDrive, BYTE * pSectorBuffer)
  518. {
  519. WORD i;
  520. int nError;
  521. BYTE bStatus;
  522. BYTE bErrorReg;
  523. BYTE bDevice;
  524. WORD wConfig;
  525. DWORD dwTotalSectors;
  526. nError = IDE_ERROR;
  527. bDevice = pDrive->bDevice;
  528. if (SelectDevice(bDevice) == IDE_OK) {
  529. ClearEvent(&hIDEEvent);
  530. IDEOut(COMMAND_REG, COMMAND_DEVICE_INFO_P);
  531. if (WaitForInterrupt(DISKTIMEOUT) == IDE_OK) {
  532. if (gbIntStatus & STATUS_DATA_REQUEST) {
  533. for (i = 0; i < IDE_SECTOR_SIZE; i = i + 2) {
  534. pSectorBuffer[i] = pIDE[DATA_READ_REG_LOW];
  535. if (pDrive->bIDEMode == MEM_8BIT_COMPACT_FLASH) {
  536. pSectorBuffer[i + 1] = pIDE[DATA_READ_REG_LOW];
  537. } else {
  538. pSectorBuffer[i + 1] = pIDE[DATA_READ_REG_HIGH];
  539. }
  540. }
  541. //
  542. // Status register is read to clear
  543. // pending interrupts.
  544. //
  545. bStatus = IDEIn(STATUS_REG);
  546. pDrive->wFlags = 0;
  547. wConfig = *(WORD *) & pSectorBuffer[0];
  548. if ((wConfig & ATAPI_CFG_INTRQ) == ATAPI_USE_INTRQ) {
  549. pDrive->wFlags |= IDE_SUPPORT_INTRQ_PACKET;
  550. }
  551. if ((wConfig & ATAPI_CFG_DEVICE) == ATAPI_IS_DIRECT_ACCESS) {
  552. pDrive->wFlags |= IDE_ZIP_DEVICE;
  553. }
  554. if ((wConfig & ATAPI_CFG_DEVICE) == ATAPI_IS_CDROM) {
  555. pDrive->wFlags |= (IDE_READ_ONLY | IDE_CDROM_DEVICE);
  556. }
  557. if ((wConfig & ATAPI_CFG_ATAPI) == ATAPI_IS_PACKET) {
  558. pDrive->wFlags |= IDE_SUPPORT_PACKET;
  559. }
  560. if ((wConfig & ATAPI_CFG_12_BYTE_MSK) == ATAPI_IS_12_BYTE) {
  561. pDrive->wFlags |= IDE_READY;
  562. nError = ATAPIGetTotalSectors(pDrive, pSectorBuffer, &dwTotalSectors);
  563. if (nError == IDE_OK) {
  564. pDrive->dwTotalSectors = dwTotalSectors;
  565. //
  566. // ModeSense
  567. //
  568. ATAPI_CMD(0x5A);
  569. aATAPICmd[2] = 0x2A;
  570. aATAPICmd[7] = 0x08;
  571. aATAPICmd[8] = 0x00;
  572. ATAPISendCommand(pDrive, pSectorBuffer, &i);
  573. //
  574. // Set Speed to 150KByte
  575. //
  576. IDEATAPISetCDSpeed(bDevice, 150);
  577. } else {
  578. pDrive->dwTotalSectors = 0;
  579. }
  580. //ATAPITest(pDrive, pSectorBuffer);
  581. }
  582. }
  583. if (gbIntStatus & STATUS_ERROR) {
  584. bErrorReg = IDEIn(ERROR_REG);
  585. }
  586. }
  587. }
  588. return (nError);
  589. }
  590. #endif
  591. /************************************************************/
  592. /* GetDeviceInfo */
  593. /* */
  594. /* PIO data in command protocol (see ATAPI-4 r17 p224 9.7) */
  595. /* */
  596. /* Return: IDE_OK, IDE_ERROR */
  597. /************************************************************/
  598. static int GetDeviceInfo(LPDRIVE pDrive, BYTE * pSectorBuffer)
  599. {
  600. WORD i;
  601. int nError;
  602. BYTE bStatus;
  603. BYTE bErrorReg;
  604. BYTE bDevice;
  605. IDEDEVICEINFO *pInfo;
  606. nError = IDE_ERROR;
  607. bDevice = pDrive->bDevice;
  608. if (SelectDevice(bDevice) == IDE_OK) {
  609. ClearEvent(&hIDEEvent);
  610. #if 0
  611. IDEOut(FEATURE_REG, 0);
  612. IDEOut(SECTOR_COUNT_REG, 1);
  613. IDEOut(SECTOR_REG, 0);
  614. IDEOut(CYLINDER_LOW_REG, 0);
  615. IDEOut(CYLINDER_HIGH_REG, 0);
  616. #endif
  617. IDEOut(COMMAND_REG, COMMAND_DEVICE_INFO);
  618. if (WaitForInterrupt(DISKTIMEOUT) == IDE_OK) {
  619. if (gbIntStatus & STATUS_DATA_REQUEST) {
  620. for (i = 0; i < IDE_SECTOR_SIZE; i = i + 2) {
  621. pSectorBuffer[i] = pIDE[DATA_READ_REG_LOW];
  622. if (pDrive->bIDEMode == MEM_8BIT_COMPACT_FLASH) {
  623. pSectorBuffer[i + 1] = pIDE[DATA_READ_REG_LOW];
  624. } else {
  625. pSectorBuffer[i + 1] = pIDE[DATA_READ_REG_HIGH];
  626. }
  627. }
  628. //
  629. // Status register is read to clear
  630. // pending interrupts.
  631. //
  632. bStatus = IDEIn(STATUS_REG);
  633. pInfo = (IDEDEVICEINFO *) pSectorBuffer;
  634. #if (IDE_SUPPORT_CHS == 1)
  635. pDrive->wCylinders = (WORD) pInfo->Cylinders;
  636. pDrive->wHeads = (BYTE) pInfo->Heads;
  637. pDrive->wSectorsPerTrack = (BYTE) pInfo->SectorsPerTrack;
  638. pDrive->dwOneSide = pDrive->wCylinders * pDrive->wSectorsPerTrack;
  639. #endif
  640. if (pInfo->LBASectors > 0) {
  641. pDrive->wFlags = IDE_READY | IDE_SUPPORT_LBA;
  642. pDrive->dwTotalSectors = pInfo->LBASectors;
  643. } else {
  644. #if (IDE_SUPPORT_CHS == 1)
  645. pDrive->wFlags = IDE_READY;
  646. pDrive->dwTotalSectors = pInfo->Cylinders;
  647. pDrive->dwTotalSectors *= pInfo->Heads;
  648. pDrive->dwTotalSectors *= pInfo->SectorsPerTrack;
  649. #endif
  650. }
  651. nError = IDE_OK;
  652. }
  653. if (gbIntStatus & STATUS_ERROR) {
  654. bErrorReg = IDEIn(ERROR_REG);
  655. }
  656. }
  657. }
  658. return (nError);
  659. }
  660. /************************************************************/
  661. /* IsPacketDevice */
  662. /* */
  663. /* Check Command block register, */
  664. /* see page 210, 9.1 Signature and persistence */
  665. /* */
  666. /* Return: TRUE, FALSE */
  667. /************************************************************/
  668. #if (IDE_SUPPORT_ATAPI == 1)
  669. static int IsPacketDevice(void)
  670. {
  671. int nPacketDevice;
  672. BYTE bSectorCount;
  673. BYTE bSectorNumber;
  674. BYTE bCylinderLow;
  675. BYTE bCylinderHigh;
  676. nPacketDevice = FALSE;
  677. bSectorCount = IDEIn(SECTOR_COUNT_REG);
  678. bSectorNumber = IDEIn(SECTOR_REG);
  679. bCylinderLow = IDEIn(CYLINDER_LOW_REG);
  680. bCylinderHigh = IDEIn(CYLINDER_HIGH_REG);
  681. if ((bCylinderLow == 0x14) && (bCylinderHigh == 0xEB)) {
  682. nPacketDevice = TRUE;
  683. }
  684. return (nPacketDevice);
  685. }
  686. #else
  687. static int IsPacketDevice(void)
  688. {
  689. return (FALSE);
  690. }
  691. #endif
  692. /************************************************************/
  693. /* DeviceDiag */
  694. /* */
  695. /* NON-data command protocol (see ATAPI-4 r17 p231 9.9) */
  696. /* */
  697. /* Return: IDE_OK, IDE_ERROR */
  698. /************************************************************/
  699. static int DeviceDiag(LPDRIVE pDrive)
  700. {
  701. int nError;
  702. BYTE bResult;
  703. nError = IDE_ERROR;
  704. if (SelectDevice(0) == IDE_OK) {
  705. #if 0
  706. IDEOut(FEATURE_REG, 0);
  707. IDEOut(SECTOR_COUNT_REG, 1);
  708. IDEOut(SECTOR_REG, 0);
  709. IDEOut(CYLINDER_LOW_REG, 0);
  710. IDEOut(CYLINDER_HIGH_REG, 0);
  711. #endif
  712. ClearEvent(&hIDEEvent);
  713. //
  714. // Check if we are a PACKET device
  715. //
  716. if (IsPacketDevice() == TRUE) {
  717. nError = IDE_OK;
  718. pDrive->wFlags = IDE_SUPPORT_PACKET;
  719. } else {
  720. IDEOut(COMMAND_REG, COMMAND_DIAG);
  721. if (WaitForInterrupt(INITTIMEOUT) == IDE_OK) {
  722. bResult = IDEIn(ERROR_REG);
  723. if (bResult == 0x01) { // see ATAPI-4 r17 p72
  724. nError = IDE_OK;
  725. }
  726. }
  727. }
  728. }
  729. return (nError);
  730. }
  731. /************************************************************/
  732. /* ReadSectors */
  733. /* */
  734. /* PIO data in command protocol, see ATAPI-4 r17 p224 9.7 */
  735. /* */
  736. /* Return: IDE_OK, IDE_ERROR */
  737. /************************************************************/
  738. static int ReadSectors(BYTE bDevice, BYTE * pData, DWORD dwStartSector, WORD wSectorCount)
  739. {
  740. WORD i, x;
  741. int nError;
  742. BYTE bValue;
  743. BYTE bErrorReg;
  744. BYTE bStatus;
  745. LPDRIVE pDrive;
  746. #if (IDE_SUPPORT_CHS == 1)
  747. WORD wCHSSector;
  748. WORD wCHSHead;
  749. WORD wCHSCylinder;
  750. #endif
  751. pDrive = &sDrive[bDevice];
  752. nError = SelectDevice(bDevice);
  753. #if (IDE_SUPPORT_CHS == 0)
  754. //
  755. // NO CHS support
  756. //
  757. if ((pDrive->wFlags & IDE_SUPPORT_LBA) == 0) {
  758. //
  759. // If we have a CHS device, this is an ERROR
  760. //
  761. nError = IDE_ERROR;
  762. }
  763. #endif
  764. if (nError == IDE_OK) {
  765. ClearEvent(&hIDEEvent);
  766. if (pDrive->wFlags & IDE_SUPPORT_LBA) {
  767. //
  768. // LBA
  769. //
  770. bValue = (BYTE) ((bDevice << 4) | 0xE0 | (dwStartSector >> 24));
  771. IDEOut(DISK_HEAD_REG, bValue);
  772. IDEOut(LBA_16_23, (BYTE) (dwStartSector >> 16));
  773. IDEOut(LBA_8_15, (BYTE) (dwStartSector >> 8));
  774. IDEOut(LBA_0_7, (BYTE) (dwStartSector));
  775. IDEOut(SECTOR_COUNT_REG, (BYTE) (wSectorCount & 0xff));
  776. IDEOut(COMMAND_REG, COMMAND_READ_SECTORS);
  777. #if (IDE_SUPPORT_CHS == 1)
  778. } else {
  779. //
  780. // CHS
  781. //
  782. wCHSSector = (WORD) (dwStartSector % pDrive->wSectorsPerTrack) + 1;
  783. wCHSHead = (WORD) (dwStartSector / pDrive->dwOneSide);
  784. dwStartSector = dwStartSector % pDrive->dwOneSide;
  785. wCHSCylinder = (WORD) (dwStartSector / pDrive->wSectorsPerTrack);
  786. wCHSHead |= (bDevice << 4) | 0xA0;
  787. IDEOut(DISK_HEAD_REG, (BYTE) wCHSHead);
  788. IDEOut(SECTOR_REG, (BYTE) wCHSSector);
  789. IDEOut(CYLINDER_LOW_REG, (BYTE) (wCHSCylinder & 0x00FF));
  790. IDEOut(CYLINDER_HIGH_REG, (BYTE) (wCHSCylinder >> 8));
  791. IDEOut(SECTOR_COUNT_REG, (BYTE) (wSectorCount & 0xff));
  792. IDEOut(COMMAND_REG, COMMAND_READ_SECTORS);
  793. #endif
  794. }
  795. for (i = 0; i < wSectorCount; i++) {
  796. nError = WaitForInterrupt(DISKTIMEOUT);
  797. if (nError == IDE_OK) {
  798. bStatus = gbIntStatus;
  799. if (bStatus & STATUS_ERROR) {
  800. bErrorReg = IDEIn(ERROR_REG);
  801. nError = IDE_ERROR;
  802. break;
  803. }
  804. for (x = 0; x < IDE_SECTOR_SIZE; x = x + 2) {
  805. pData[x] = pIDE[DATA_READ_REG_LOW];
  806. if (pDrive->bIDEMode == MEM_8BIT_COMPACT_FLASH) {
  807. pData[x + 1] = pIDE[DATA_READ_REG_LOW];
  808. } else {
  809. pData[x + 1] = pIDE[DATA_READ_REG_HIGH];
  810. }
  811. }
  812. pData += IDE_SECTOR_SIZE;
  813. } else {
  814. bStatus = IDEIn(STATUS_REG);
  815. bErrorReg = IDEIn(ERROR_REG);
  816. }
  817. }
  818. bStatus = IDEIn(STATUS_REG);
  819. }
  820. /* SelectDevice */
  821. return (nError);
  822. }
  823. #if (IDE_SUPPORT_WRITE == 1)
  824. /************************************************************/
  825. /* WriteSectors */
  826. /* */
  827. /* PIO data out command protocol, see ATAPI-4 r17 p227 9.8 */
  828. /* */
  829. /* Return: IDE_OK, IDE_ERROR */
  830. /************************************************************/
  831. static BYTE WriteSectors(WORD bDevice, BYTE * pData, DWORD dwStartSector, WORD wSectorCount)
  832. {
  833. WORD i;
  834. int x;
  835. int nError;
  836. BYTE bValue;
  837. BYTE bStatus;
  838. BYTE bErrorReg;
  839. LPDRIVE pDrive;
  840. #if (IDE_SUPPORT_CHS == 1)
  841. WORD wCHSSector;
  842. WORD wCHSHead;
  843. WORD wCHSCylinder;
  844. #endif
  845. pDrive = &sDrive[bDevice];
  846. nError = SelectDevice(bDevice);
  847. #if (IDE_SUPPORT_CHS == 0)
  848. //
  849. // NO CHS support
  850. //
  851. if ((pDrive->wFlags & IDE_SUPPORT_LBA) == 0) {
  852. //
  853. // If we found a CHS device, this is an ERROR
  854. //
  855. nError = IDE_ERROR;
  856. }
  857. #endif
  858. if (nError == IDE_OK) {
  859. if (pDrive->wFlags & IDE_SUPPORT_LBA) {
  860. //
  861. // LBA
  862. //
  863. bValue = (BYTE) ((bDevice << 4) | 0xE0 | (dwStartSector >> 24));
  864. IDEOut(DISK_HEAD_REG, bValue);
  865. IDEOut(LBA_16_23, (BYTE) (dwStartSector >> 16));
  866. IDEOut(LBA_8_15, (BYTE) (dwStartSector >> 8));
  867. IDEOut(LBA_0_7, (BYTE) (dwStartSector));
  868. IDEOut(SECTOR_COUNT_REG, (BYTE) (wSectorCount & 0xff));
  869. IDEOut(COMMAND_REG, COMMAND_WRITE_SECTORS);
  870. #if (IDE_SUPPORT_CHS == 1)
  871. } else {
  872. //
  873. // CHS
  874. //
  875. wCHSSector = (WORD) (dwStartSector % pDrive->wSectorsPerTrack) + 1;
  876. wCHSHead = (WORD) (dwStartSector / pDrive->dwOneSide);
  877. dwStartSector = dwStartSector % pDrive->dwOneSide;
  878. wCHSCylinder = (WORD) (dwStartSector / pDrive->wSectorsPerTrack);
  879. wCHSHead |= (bDevice << 4) | 0xA0;
  880. IDEOut(DISK_HEAD_REG, (BYTE) wCHSHead);
  881. IDEOut(SECTOR_REG, (BYTE) wCHSSector);
  882. IDEOut(CYLINDER_LOW_REG, (BYTE) (wCHSCylinder & 0x00FF));
  883. IDEOut(CYLINDER_HIGH_REG, (BYTE) (wCHSCylinder >> 8));
  884. IDEOut(SECTOR_COUNT_REG, (BYTE) (wSectorCount & 0xff));
  885. IDEOut(COMMAND_REG, COMMAND_WRITE_SECTORS);
  886. #endif
  887. }
  888. if (WaitDRQ(CONTROLLERTIMEOUT) == IDE_OK) {
  889. bStatus = IDEIn(STATUS_REG);
  890. for (i = 0; i < wSectorCount; i++) {
  891. for (x = 0; x < IDE_SECTOR_SIZE; x = x + 2) {
  892. if (pDrive->bIDEMode == MEM_8BIT_COMPACT_FLASH) {
  893. IDEOut(DATA_WRITE_REG_LOW, pData[x]);
  894. IDEOut(DATA_WRITE_REG_LOW, pData[x + 1]);
  895. } else {
  896. IDEOut(DATA_WRITE_REG_HIGH, pData[x + 1]);
  897. IDEOut(DATA_WRITE_REG_LOW, pData[x]);
  898. }
  899. }
  900. pData += IDE_SECTOR_SIZE;
  901. nError = WaitForInterrupt(DISKTIMEOUT);
  902. if (nError == IDE_OK) {
  903. bStatus = gbIntStatus;
  904. if ((bStatus & STATUS_ERROR) == STATUS_ERROR) {
  905. bErrorReg = IDEIn(ERROR_REG);
  906. nError = IDE_ERROR;
  907. break;
  908. }
  909. } else {
  910. //
  911. // Error
  912. //
  913. nError = IDE_ERROR;
  914. break;
  915. }
  916. }
  917. } else {
  918. //
  919. // Device: Set error status (D)
  920. //
  921. WaitForInterrupt(DISKTIMEOUT);
  922. bErrorReg = IDEIn(ERROR_REG);
  923. } /* endif WaitDRQ */
  924. }
  925. /* SelectDevice */
  926. return (nError);
  927. }
  928. #endif
  929. /************************************************************/
  930. /* CFChange */
  931. /************************************************************/
  932. THREAD(CFChange, arg)
  933. {
  934. BYTE bNewStatus;
  935. BYTE *pSectorBuffer;
  936. while (1) {
  937. NutEventWaitNext(&hCFChangeInt, 0);
  938. //
  939. // Wait a while, that the cf contact is stable and not to bounce.
  940. //
  941. NutSleep(2000);
  942. bNewStatus = (PINE & BV(CF_IRQ));
  943. if (bNewStatus != gbCFMountStatus) {
  944. if (bNewStatus == CF_AVAILABLE) {
  945. gbCFMountStatus = CF_AVAILABLE;
  946. //
  947. // We must mount the new CF card.
  948. //
  949. HardwareReset(&sDrive[0]);
  950. pSectorBuffer = (BYTE *) NutHeapAlloc(IDE_SECTOR_SIZE);
  951. if (pSectorBuffer != NULL) {
  952. IDEMountDevice(IDE_DRIVE_C, pSectorBuffer);
  953. NutHeapFree(pSectorBuffer);
  954. if (pUserMountFunc != NULL) {
  955. pUserMountFunc(IDE_DRIVE_C);
  956. }
  957. }
  958. NutEnterCritical();
  959. EICR &= ~CF_INT_SENS_MASK;
  960. EICR |= CF_INT_RISING_EDGE;
  961. NutExitCritical();
  962. } else {
  963. gbCFMountStatus = CF_NOT_AVAILABLE;
  964. //
  965. // The user has removed the CF card,
  966. // now UnMount the device.
  967. //
  968. IDEUnMountDevice(IDE_DRIVE_C);
  969. if (pUserUnMountFunc != NULL) {
  970. pUserUnMountFunc(IDE_DRIVE_C);
  971. }
  972. NutEnterCritical();
  973. EICR &= ~CF_INT_SENS_MASK;
  974. EICR |= CF_INT_FALLING_EDGE;
  975. NutExitCritical();
  976. }
  977. } /* endif bValue != gbCFMountStatus */
  978. } /* end while */
  979. }
  980. /*==========================================================*/
  981. /* DEFINE: All code exported */
  982. /*==========================================================*/
  983. /************************************************************/
  984. /* IDEInit */
  985. /************************************************************/
  986. int IDEInit(int nBaseAddress, int nIDEMode, IDE_MOUNT_FUNC * pMountFunc, IDE_MOUNT_FUNC * pUnMountFunc)
  987. {
  988. int i;
  989. int nError;
  990. BYTE bValue;
  991. pUserMountFunc = pMountFunc;
  992. pUserUnMountFunc = pUnMountFunc;
  993. //
  994. // If nBaseAddress is 0, we use the default address
  995. //
  996. if (nBaseAddress == 0) {
  997. nBaseAddress = IDE_BASE_ADDRESS;
  998. }
  999. //
  1000. // With MCUCR 0xC0 and XMCRA 0x02 we set:
  1001. //
  1002. // Wait two cycles during read/write and
  1003. // wait one cycle before driving out new address.
  1004. // This setting is for 0x1100 - 0xffff
  1005. //
  1006. #ifdef __AVR_ENHANCED__
  1007. XMCRA = _BV(SRW11);
  1008. #endif
  1009. //
  1010. // Enable the IDE Reset
  1011. //
  1012. DDRD = 0x20;
  1013. for (i = 0; i < IDE_MAX_SUPPORTED_DEVICE; i++) {
  1014. memset((BYTE *) & sDrive[i], 0x00, sizeof(DRIVE));
  1015. sDrive[i].bDevice = (BYTE) i;
  1016. sDrive[i].bIDEMode = (BYTE) nIDEMode;
  1017. }
  1018. IDESemaInit();
  1019. pIDE = (unsigned char *) nBaseAddress;
  1020. ClearEvent(&hIDEEvent);
  1021. NutEnterCritical();
  1022. //
  1023. // Clear interrupt status
  1024. //
  1025. gbIntStatus = 0;
  1026. //
  1027. // Install IDE interrupt
  1028. //
  1029. nError = NutRegisterIrqHandler(&sig_INTERRUPT7, IDEInterrupt, NULL);
  1030. if (nError == FALSE) {
  1031. EICR |= IDE_INT_RISING_EDGE;
  1032. sbi(EIMSK, IDE_IRQ);
  1033. }
  1034. switch (nIDEMode) {
  1035. case IDE_HARDDISK:{
  1036. break;
  1037. }
  1038. //
  1039. // If your drive does not work correct with 14.7456Mhz,
  1040. // try to devide the clock by 2.
  1041. //
  1042. // Important: You must correct the baudrate in your app.
  1043. // if you set 115200, you get only 57600.
  1044. //
  1045. case IDE_HARDDISK_7MHZ:{
  1046. #if defined(CLKPR) /* AT90MEGA128 uses a clock prescaler register */
  1047. CLKPR = _BV(CLKPCE); // Enable clock speed change
  1048. CLKPR = 2; // Run at half the clock speed
  1049. #else
  1050. XDIV = 0xff;
  1051. #endif
  1052. break;
  1053. }
  1054. case IDE_COMPACT_FLASH:{
  1055. break;
  1056. }
  1057. case MEM_8BIT_COMPACT_FLASH:{
  1058. //
  1059. // Now we will support to change the CF-Card in
  1060. // a running system, therefore we must install
  1061. // a INT handler and some other parts :-)
  1062. //
  1063. //
  1064. // Clear CF_INT_SENS_MASK.
  1065. //
  1066. EICR &= ~CF_INT_SENS_MASK;
  1067. //
  1068. // Check the status of the compact flash.
  1069. //
  1070. bValue = (PINE & BV(CF_IRQ));
  1071. if (bValue == CF_AVAILABLE) {
  1072. gbCFMountStatus = CF_AVAILABLE;
  1073. //
  1074. // The rising edge between two samples
  1075. // of INTX generates an interrupt request.
  1076. //
  1077. EICR |= CF_INT_RISING_EDGE;
  1078. } else {
  1079. gbCFMountStatus = CF_NOT_AVAILABLE;
  1080. //
  1081. // The falling edge between two samples
  1082. // of INTX generates an interrupt request.
  1083. //
  1084. EICR |= CF_INT_FALLING_EDGE;
  1085. }
  1086. nError = NutRegisterIrqHandler(&sig_INTERRUPT6, CFInterrupt, NULL);
  1087. if (nError == FALSE) {
  1088. NutThreadCreate("cfchange", CFChange, NULL, 640);
  1089. sbi(EIMSK, CF_IRQ);
  1090. }
  1091. break;
  1092. }
  1093. }
  1094. NutExitCritical();
  1095. HardwareReset(&sDrive[0]);
  1096. nError = IDE_OK;
  1097. return (nError);
  1098. }
  1099. /************************************************************/
  1100. /* IDEMountDevice */
  1101. /************************************************************/
  1102. int IDEMountDevice(BYTE bDevice, BYTE * pSectorBuffer)
  1103. {
  1104. int nError;
  1105. LPDRIVE pDrive;
  1106. pDrive = NULL;
  1107. IDELock();
  1108. if (bDevice >= IDE_MAX_SUPPORTED_DEVICE) {
  1109. nError = IDE_ERROR;
  1110. } else {
  1111. pDrive = &sDrive[bDevice];
  1112. pDrive->wFlags = 0;
  1113. #if (IDE_SUPPORT_CHS == 1)
  1114. //
  1115. // CHS values
  1116. //
  1117. pDrive->wCylinders = 0;
  1118. pDrive->wHeads = 0;
  1119. pDrive->wSectorsPerTrack = 0;
  1120. #endif
  1121. //
  1122. // LBA value
  1123. //
  1124. pDrive->dwTotalSectors = 0;
  1125. nError = DeviceDiag(pDrive);
  1126. if (pDrive->wFlags & IDE_SUPPORT_PACKET) {
  1127. #if (IDE_SUPPORT_ATAPI == 1)
  1128. pDrive->wSectorSize = ATAPI_SECTOR_SIZE;
  1129. nError = GetDeviceInfoPacket(pDrive, pSectorBuffer);
  1130. if (pDrive->wFlags & IDE_READY) {
  1131. nError = IDE_OK;
  1132. }
  1133. #endif
  1134. } else {
  1135. if (nError != IDE_OK) {
  1136. //
  1137. // Try it again
  1138. //
  1139. nError = DeviceDiag(pDrive);
  1140. }
  1141. if (nError == IDE_OK) {
  1142. pDrive->wSectorSize = IDE_SECTOR_SIZE;
  1143. nError = GetDeviceInfo(pDrive, pSectorBuffer);
  1144. if (pDrive->wFlags & IDE_READY) {
  1145. nError = IDE_OK;
  1146. }
  1147. }
  1148. }
  1149. } /* (bDevice >= IDE_MAX_SUPPORTED_DEVICE) */
  1150. IDEFree();
  1151. return (nError);
  1152. }
  1153. /************************************************************/
  1154. /* IDEGetSectorSize */
  1155. /************************************************************/
  1156. int IDEGetSectorSize(BYTE bDevice)
  1157. {
  1158. int nSectorSize;
  1159. LPDRIVE pDrive;
  1160. nSectorSize = 0;
  1161. IDELock();
  1162. if (bDevice >= IDE_MAX_SUPPORTED_DEVICE) {
  1163. nSectorSize = 0;
  1164. } else {
  1165. pDrive = &sDrive[bDevice];
  1166. nSectorSize = pDrive->wSectorSize;
  1167. }
  1168. IDEFree();
  1169. return (nSectorSize);
  1170. }
  1171. /************************************************************/
  1172. /* IDEIsCDROMDevice */
  1173. /************************************************************/
  1174. int IDEIsCDROMDevice(BYTE bDevice)
  1175. {
  1176. int nIsCDROM;
  1177. LPDRIVE pDrive;
  1178. nIsCDROM = FALSE;
  1179. IDELock();
  1180. if (bDevice >= IDE_MAX_SUPPORTED_DEVICE) {
  1181. nIsCDROM = FALSE;
  1182. } else {
  1183. pDrive = &sDrive[bDevice];
  1184. if ((pDrive->wFlags & IDE_READY) && (pDrive->wFlags & IDE_CDROM_DEVICE)) {
  1185. nIsCDROM = TRUE;
  1186. }
  1187. }
  1188. IDEFree();
  1189. return (nIsCDROM);
  1190. }
  1191. /************************************************************/
  1192. /* IDEIsZIPDevice */
  1193. /************************************************************/
  1194. int IDEIsZIPDevice(BYTE bDevice)
  1195. {
  1196. int nIsZIP;
  1197. LPDRIVE pDrive;
  1198. nIsZIP = FALSE;
  1199. IDELock();
  1200. if (bDevice >= IDE_MAX_SUPPORTED_DEVICE) {
  1201. nIsZIP = FALSE;
  1202. } else {
  1203. pDrive = &sDrive[bDevice];
  1204. if ((pDrive->wFlags & IDE_READY) && (pDrive->wFlags & IDE_ZIP_DEVICE)) {
  1205. nIsZIP = TRUE;
  1206. }
  1207. }
  1208. IDEFree();
  1209. return (nIsZIP);
  1210. }
  1211. /************************************************************/
  1212. /* IDEUnMountDevice */
  1213. /************************************************************/
  1214. int IDEUnMountDevice(BYTE bDevice)
  1215. {
  1216. int nError = 0;
  1217. LPDRIVE pDrive;
  1218. pDrive = NULL;
  1219. IDELock();
  1220. if (bDevice >= IDE_MAX_SUPPORTED_DEVICE) {
  1221. nError = IDE_ERROR;
  1222. } else {
  1223. pDrive = &sDrive[bDevice];
  1224. pDrive->wFlags = 0;
  1225. }
  1226. IDEFree();
  1227. return (nError);
  1228. }
  1229. /************************************************************/
  1230. /* IDEGetTotalSectors */
  1231. /************************************************************/
  1232. DWORD IDEGetTotalSectors(BYTE bDevice)
  1233. {
  1234. DWORD dwTotalSectors;
  1235. LPDRIVE pDrive;
  1236. dwTotalSectors = 0;
  1237. IDELock();
  1238. if (bDevice >= IDE_MAX_SUPPORTED_DEVICE) {
  1239. dwTotalSectors = 0;
  1240. } else {
  1241. pDrive = &sDrive[bDevice];
  1242. dwTotalSectors = pDrive->dwTotalSectors;
  1243. dwTotalSectors -= 64;
  1244. }
  1245. IDEFree();
  1246. return (dwTotalSectors);
  1247. }
  1248. /************************************************************/
  1249. /* IDEReadSectors */
  1250. /************************************************************/
  1251. int IDEReadSectors(BYTE bDevice, void *pData, DWORD dwStartSector, WORD wSectorCount)
  1252. {
  1253. WORD i;
  1254. int nError;
  1255. WORD wReadCount;
  1256. LPDRIVE pDrive = 0;
  1257. BYTE *pByte;
  1258. nError = IDE_OK;
  1259. IDELock();
  1260. if (bDevice >= IDE_MAX_SUPPORTED_DEVICE) {
  1261. nError = IDE_DRIVE_NOT_FOUND;
  1262. } else {
  1263. pDrive = &sDrive[bDevice];
  1264. if ((pDrive->wFlags & IDE_READY) == 0) {
  1265. nError = IDE_DRIVE_NOT_FOUND;
  1266. } else {
  1267. if ((dwStartSector + wSectorCount) > pDrive->dwTotalSectors) {
  1268. nError = IDE_PARAM_ERROR;
  1269. }
  1270. }
  1271. }
  1272. if ((pDrive->wFlags & IDE_SUPPORT_PACKET) && ((wSectorCount > 1))) {
  1273. //
  1274. // Sorry, in this version we support
  1275. // only 1 sector for PACKET devices.
  1276. //
  1277. nError = IDE_PARAM_ERROR;
  1278. }
  1279. if ((pDrive->wFlags & IDE_SUPPORT_PACKET) && (nError == IDE_OK)) {
  1280. #if (IDE_SUPPORT_ATAPI == 1)
  1281. //
  1282. // ATAPI
  1283. //
  1284. ATAPI_CMD(ATAPI_CMD_READ12);
  1285. i = 5;
  1286. while (dwStartSector) {
  1287. aATAPICmd[i--] = (BYTE) dwStartSector;
  1288. dwStartSector >>= 8;
  1289. }
  1290. //
  1291. // Reading one sector only
  1292. //
  1293. aATAPICmd[9] = 1;
  1294. nError = ATAPISendCommand(pDrive, pData, &i);
  1295. if ((nError != IDE_OK) || (i != pDrive->wSectorSize)) {
  1296. nError = IDE_ERROR;
  1297. }
  1298. #else
  1299. nError = IDE_ERROR;
  1300. #endif
  1301. } else {
  1302. if (nError == IDE_OK) {
  1303. pByte = (BYTE *) pData;
  1304. while (wSectorCount > 0) {
  1305. if (wSectorCount < 256) {
  1306. wReadCount = wSectorCount;
  1307. } else {
  1308. wReadCount = 256;
  1309. }
  1310. nError = ReadSectors(bDevice, pByte, dwStartSector, wReadCount);
  1311. if (nError != IDE_OK) {
  1312. break;
  1313. }
  1314. dwStartSector += wReadCount;
  1315. wSectorCount -= wReadCount;
  1316. pByte += (wReadCount * pDrive->wSectorSize);
  1317. }
  1318. }
  1319. }
  1320. IDEFree();
  1321. return (nError);
  1322. }
  1323. #if (IDE_SUPPORT_WRITE == 1)
  1324. /************************************************************/
  1325. /* IDEWriteSectors */
  1326. /************************************************************/
  1327. int IDEWriteSectors(BYTE bDevice, void *pData, DWORD dwStartSector, WORD wSectorCount)
  1328. {
  1329. int nError;
  1330. WORD wWriteCount;
  1331. LPDRIVE pDrive;
  1332. BYTE *pByte;
  1333. nError = IDE_OK;
  1334. pDrive = NULL;
  1335. IDELock();
  1336. if (bDevice >= IDE_MAX_SUPPORTED_DEVICE) {
  1337. nError = IDE_DRIVE_NOT_FOUND;
  1338. } else {
  1339. pDrive = &sDrive[bDevice];
  1340. if ((dwStartSector + wSectorCount) > pDrive->dwTotalSectors) {
  1341. nError = IDE_PARAM_ERROR;
  1342. }
  1343. if ((pDrive->wFlags & IDE_READY) == 0) {
  1344. nError = IDE_DRIVE_NOT_FOUND;
  1345. }
  1346. if (pDrive->wFlags & IDE_READ_ONLY) {
  1347. nError = IDE_NOT_SUPPORTED;
  1348. }
  1349. }
  1350. if (nError == IDE_OK) {
  1351. pByte = (BYTE *) pData;
  1352. while (wSectorCount > 0) {
  1353. if (wSectorCount < 256) {
  1354. wWriteCount = wSectorCount;
  1355. } else {
  1356. wWriteCount = 256;
  1357. }
  1358. nError = WriteSectors(bDevice, pByte, dwStartSector, wWriteCount);
  1359. if (nError != IDE_OK) {
  1360. break;
  1361. }
  1362. dwStartSector += wWriteCount;
  1363. wSectorCount -= wWriteCount;
  1364. pByte += (wWriteCount * IDE_SECTOR_SIZE);
  1365. }
  1366. }
  1367. IDEFree();
  1368. return (nError);
  1369. }
  1370. #endif
  1371. #if (IDE_SUPPORT_ATAPI == 1)
  1372. /************************************************************/
  1373. /* IDEATAPISetCDSpeed */
  1374. /************************************************************/
  1375. int IDEATAPISetCDSpeed(BYTE bDevice, WORD wSpeed)
  1376. {
  1377. WORD i;
  1378. int nError;
  1379. LPDRIVE pDrive;
  1380. nError = IDE_OK;
  1381. pDrive = NULL;
  1382. if (bDevice >= IDE_MAX_SUPPORTED_DEVICE) {
  1383. nError = IDE_DRIVE_NOT_FOUND;
  1384. } else {
  1385. pDrive = &sDrive[bDevice];
  1386. if ((pDrive->wFlags & IDE_READY) == 0) {
  1387. nError = IDE_DRIVE_NOT_FOUND;
  1388. }
  1389. if ((pDrive->wFlags & IDE_SUPPORT_PACKET) == 0) {
  1390. nError = IDE_NOT_SUPPORTED;
  1391. }
  1392. }
  1393. if (nError == IDE_OK) {
  1394. ATAPI_CMD(0xBB);
  1395. aATAPICmd[2] = (BYTE) ((wSpeed >> 8) & 0x00FF);
  1396. aATAPICmd[3] = (BYTE) (wSpeed & 0x00FF);
  1397. nError = ATAPISendCommand(pDrive, NULL, &i);
  1398. }
  1399. return (nError);
  1400. }
  1401. #endif