cmd_ide.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  1. /*
  2. * (C) Copyright 2000-2005
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * IDE support
  26. */
  27. #include <common.h>
  28. #include <config.h>
  29. #include <watchdog.h>
  30. #include <command.h>
  31. #include <image.h>
  32. #include <asm/byteorder.h>
  33. #include <asm/io.h>
  34. #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
  35. # include <pcmcia.h>
  36. #endif
  37. #ifdef CONFIG_8xx
  38. # include <mpc8xx.h>
  39. #endif
  40. #ifdef CONFIG_MPC5xxx
  41. #include <mpc5xxx.h>
  42. #endif
  43. #include <ide.h>
  44. #include <ata.h>
  45. #ifdef CONFIG_STATUS_LED
  46. # include <status_led.h>
  47. #endif
  48. #ifdef CONFIG_IDE_8xx_DIRECT
  49. DECLARE_GLOBAL_DATA_PTR;
  50. #endif
  51. #ifdef __PPC__
  52. # define EIEIO __asm__ volatile ("eieio")
  53. # define SYNC __asm__ volatile ("sync")
  54. #else
  55. # define EIEIO /* nothing */
  56. # define SYNC /* nothing */
  57. #endif
  58. #ifdef CONFIG_IDE_8xx_DIRECT
  59. /* Timings for IDE Interface
  60. *
  61. * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
  62. * 70 165 30 PIO-Mode 0, [ns]
  63. * 4 9 2 [Cycles]
  64. * 50 125 20 PIO-Mode 1, [ns]
  65. * 3 7 2 [Cycles]
  66. * 30 100 15 PIO-Mode 2, [ns]
  67. * 2 6 1 [Cycles]
  68. * 30 80 10 PIO-Mode 3, [ns]
  69. * 2 5 1 [Cycles]
  70. * 25 70 10 PIO-Mode 4, [ns]
  71. * 2 4 1 [Cycles]
  72. */
  73. const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
  74. {
  75. /* Setup Length Hold */
  76. { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
  77. { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
  78. { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
  79. { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
  80. { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
  81. };
  82. static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
  83. #ifndef CFG_PIO_MODE
  84. #define CFG_PIO_MODE 0 /* use a relaxed default */
  85. #endif
  86. static int pio_mode = CFG_PIO_MODE;
  87. /* Make clock cycles and always round up */
  88. #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
  89. #endif /* CONFIG_IDE_8xx_DIRECT */
  90. /* ------------------------------------------------------------------------- */
  91. /* Current I/O Device */
  92. static int curr_device = -1;
  93. /* Current offset for IDE0 / IDE1 bus access */
  94. ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
  95. #if defined(CFG_ATA_IDE0_OFFSET)
  96. CFG_ATA_IDE0_OFFSET,
  97. #endif
  98. #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
  99. CFG_ATA_IDE1_OFFSET,
  100. #endif
  101. };
  102. #ifndef CONFIG_AMIGAONEG3SE
  103. static int ide_bus_ok[CFG_IDE_MAXBUS];
  104. #else
  105. static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
  106. #endif
  107. block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
  108. /* ------------------------------------------------------------------------- */
  109. #ifdef CONFIG_IDE_LED
  110. #if !defined(CONFIG_KUP4K) && !defined(CONFIG_KUP4X) &&!defined(CONFIG_BMS2003) &&!defined(CONFIG_CPC45)
  111. static void ide_led (uchar led, uchar status);
  112. #else
  113. extern void ide_led (uchar led, uchar status);
  114. #endif
  115. #else
  116. #ifndef CONFIG_AMIGAONEG3SE
  117. #define ide_led(a,b) /* dummy */
  118. #else
  119. extern void ide_led(uchar led, uchar status);
  120. #define LED_IDE1 1
  121. #define LED_IDE2 2
  122. #define CONFIG_IDE_LED 1
  123. #define DEVICE_LED(x) 1
  124. #endif
  125. #endif
  126. #ifdef CONFIG_IDE_RESET
  127. static void ide_reset (void);
  128. #else
  129. #define ide_reset() /* dummy */
  130. #endif
  131. static void ide_ident (block_dev_desc_t *dev_desc);
  132. static uchar ide_wait (int dev, ulong t);
  133. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  134. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  135. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  136. static void input_data(int dev, ulong *sect_buf, int words);
  137. static void output_data(int dev, ulong *sect_buf, int words);
  138. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
  139. #ifndef CFG_ATA_PORT_ADDR
  140. #define CFG_ATA_PORT_ADDR(port) (port)
  141. #endif
  142. #ifdef CONFIG_ATAPI
  143. static void atapi_inquiry(block_dev_desc_t *dev_desc);
  144. ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
  145. #endif
  146. #ifdef CONFIG_IDE_8xx_DIRECT
  147. static void set_pcmcia_timing (int pmode);
  148. #endif
  149. /* ------------------------------------------------------------------------- */
  150. int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  151. {
  152. int rcode = 0;
  153. switch (argc) {
  154. case 0:
  155. case 1:
  156. printf ("Usage:\n%s\n", cmdtp->usage);
  157. return 1;
  158. case 2:
  159. if (strncmp(argv[1],"res",3) == 0) {
  160. puts ("\nReset IDE"
  161. #ifdef CONFIG_IDE_8xx_DIRECT
  162. " on PCMCIA " PCMCIA_SLOT_MSG
  163. #endif
  164. ": ");
  165. ide_init ();
  166. return 0;
  167. } else if (strncmp(argv[1],"inf",3) == 0) {
  168. int i;
  169. putc ('\n');
  170. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  171. if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
  172. continue; /* list only known devices */
  173. printf ("IDE device %d: ", i);
  174. dev_print(&ide_dev_desc[i]);
  175. }
  176. return 0;
  177. } else if (strncmp(argv[1],"dev",3) == 0) {
  178. if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
  179. puts ("\nno IDE devices available\n");
  180. return 1;
  181. }
  182. printf ("\nIDE device %d: ", curr_device);
  183. dev_print(&ide_dev_desc[curr_device]);
  184. return 0;
  185. } else if (strncmp(argv[1],"part",4) == 0) {
  186. int dev, ok;
  187. for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
  188. if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
  189. ++ok;
  190. if (dev)
  191. putc ('\n');
  192. print_part(&ide_dev_desc[dev]);
  193. }
  194. }
  195. if (!ok) {
  196. puts ("\nno IDE devices available\n");
  197. rcode ++;
  198. }
  199. return rcode;
  200. }
  201. printf ("Usage:\n%s\n", cmdtp->usage);
  202. return 1;
  203. case 3:
  204. if (strncmp(argv[1],"dev",3) == 0) {
  205. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  206. printf ("\nIDE device %d: ", dev);
  207. if (dev >= CFG_IDE_MAXDEVICE) {
  208. puts ("unknown device\n");
  209. return 1;
  210. }
  211. dev_print(&ide_dev_desc[dev]);
  212. /*ide_print (dev);*/
  213. if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
  214. return 1;
  215. }
  216. curr_device = dev;
  217. puts ("... is now current device\n");
  218. return 0;
  219. } else if (strncmp(argv[1],"part",4) == 0) {
  220. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  221. if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
  222. print_part(&ide_dev_desc[dev]);
  223. } else {
  224. printf ("\nIDE device %d not available\n", dev);
  225. rcode = 1;
  226. }
  227. return rcode;
  228. #if 0
  229. } else if (strncmp(argv[1],"pio",4) == 0) {
  230. int mode = (int)simple_strtoul(argv[2], NULL, 10);
  231. if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
  232. puts ("\nSetting ");
  233. pio_mode = mode;
  234. ide_init ();
  235. } else {
  236. printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
  237. mode, IDE_MAX_PIO_MODE);
  238. }
  239. return;
  240. #endif
  241. }
  242. printf ("Usage:\n%s\n", cmdtp->usage);
  243. return 1;
  244. default:
  245. /* at least 4 args */
  246. if (strcmp(argv[1],"read") == 0) {
  247. ulong addr = simple_strtoul(argv[2], NULL, 16);
  248. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  249. ulong n;
  250. #ifdef CFG_64BIT_LBA
  251. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  252. printf ("\nIDE read: device %d block # %qd, count %ld ... ",
  253. curr_device, blk, cnt);
  254. #else
  255. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  256. printf ("\nIDE read: device %d block # %ld, count %ld ... ",
  257. curr_device, blk, cnt);
  258. #endif
  259. n = ide_dev_desc[curr_device].block_read (curr_device,
  260. blk, cnt,
  261. (ulong *)addr);
  262. /* flush cache after read */
  263. flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
  264. printf ("%ld blocks read: %s\n",
  265. n, (n==cnt) ? "OK" : "ERROR");
  266. if (n==cnt) {
  267. return 0;
  268. } else {
  269. return 1;
  270. }
  271. } else if (strcmp(argv[1],"write") == 0) {
  272. ulong addr = simple_strtoul(argv[2], NULL, 16);
  273. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  274. ulong n;
  275. #ifdef CFG_64BIT_LBA
  276. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  277. printf ("\nIDE write: device %d block # %qd, count %ld ... ",
  278. curr_device, blk, cnt);
  279. #else
  280. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  281. printf ("\nIDE write: device %d block # %ld, count %ld ... ",
  282. curr_device, blk, cnt);
  283. #endif
  284. n = ide_write (curr_device, blk, cnt, (ulong *)addr);
  285. printf ("%ld blocks written: %s\n",
  286. n, (n==cnt) ? "OK" : "ERROR");
  287. if (n==cnt) {
  288. return 0;
  289. } else {
  290. return 1;
  291. }
  292. } else {
  293. printf ("Usage:\n%s\n", cmdtp->usage);
  294. rcode = 1;
  295. }
  296. return rcode;
  297. }
  298. }
  299. int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  300. {
  301. char *boot_device = NULL;
  302. char *ep;
  303. int dev, part = 0;
  304. ulong addr, cnt;
  305. disk_partition_t info;
  306. image_header_t *hdr;
  307. int rcode = 0;
  308. #if defined(CONFIG_FIT)
  309. const void *fit_hdr = NULL;
  310. #endif
  311. show_boot_progress (41);
  312. switch (argc) {
  313. case 1:
  314. addr = CFG_LOAD_ADDR;
  315. boot_device = getenv ("bootdevice");
  316. break;
  317. case 2:
  318. addr = simple_strtoul(argv[1], NULL, 16);
  319. boot_device = getenv ("bootdevice");
  320. break;
  321. case 3:
  322. addr = simple_strtoul(argv[1], NULL, 16);
  323. boot_device = argv[2];
  324. break;
  325. default:
  326. printf ("Usage:\n%s\n", cmdtp->usage);
  327. show_boot_progress (-42);
  328. return 1;
  329. }
  330. show_boot_progress (42);
  331. if (!boot_device) {
  332. puts ("\n** No boot device **\n");
  333. show_boot_progress (-43);
  334. return 1;
  335. }
  336. show_boot_progress (43);
  337. dev = simple_strtoul(boot_device, &ep, 16);
  338. if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
  339. printf ("\n** Device %d not available\n", dev);
  340. show_boot_progress (-44);
  341. return 1;
  342. }
  343. show_boot_progress (44);
  344. if (*ep) {
  345. if (*ep != ':') {
  346. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  347. show_boot_progress (-45);
  348. return 1;
  349. }
  350. part = simple_strtoul(++ep, NULL, 16);
  351. }
  352. show_boot_progress (45);
  353. if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
  354. show_boot_progress (-46);
  355. return 1;
  356. }
  357. show_boot_progress (46);
  358. if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
  359. (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
  360. printf ("\n** Invalid partition type \"%.32s\""
  361. " (expect \"" BOOT_PART_TYPE "\")\n",
  362. info.type);
  363. show_boot_progress (-47);
  364. return 1;
  365. }
  366. show_boot_progress (47);
  367. printf ("\nLoading from IDE device %d, partition %d: "
  368. "Name: %.32s Type: %.32s\n",
  369. dev, part, info.name, info.type);
  370. debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
  371. info.start, info.size, info.blksz);
  372. if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
  373. printf ("** Read error on %d:%d\n", dev, part);
  374. show_boot_progress (-48);
  375. return 1;
  376. }
  377. show_boot_progress (48);
  378. switch (genimg_get_format ((void *)addr)) {
  379. case IMAGE_FORMAT_LEGACY:
  380. hdr = (image_header_t *)addr;
  381. show_boot_progress (49);
  382. if (!image_check_hcrc (hdr)) {
  383. puts ("\n** Bad Header Checksum **\n");
  384. show_boot_progress (-50);
  385. return 1;
  386. }
  387. show_boot_progress (50);
  388. image_print_contents (hdr);
  389. cnt = image_get_image_size (hdr);
  390. break;
  391. #if defined(CONFIG_FIT)
  392. case IMAGE_FORMAT_FIT:
  393. fit_hdr = (const void *)addr;
  394. puts ("Fit image detected...\n");
  395. cnt = fit_get_size (fit_hdr);
  396. break;
  397. #endif
  398. default:
  399. show_boot_progress (-49);
  400. puts ("** Unknown image type\n");
  401. return 1;
  402. }
  403. cnt += info.blksz - 1;
  404. cnt /= info.blksz;
  405. cnt -= 1;
  406. if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
  407. (ulong *)(addr+info.blksz)) != cnt) {
  408. printf ("** Read error on %d:%d\n", dev, part);
  409. show_boot_progress (-51);
  410. return 1;
  411. }
  412. show_boot_progress (51);
  413. #if defined(CONFIG_FIT)
  414. /* This cannot be done earlier, we need complete FIT image in RAM first */
  415. if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
  416. if (!fit_check_format (fit_hdr)) {
  417. show_boot_progress (-140);
  418. puts ("** Bad FIT image format\n");
  419. return 1;
  420. }
  421. show_boot_progress (141);
  422. fit_print_contents (fit_hdr);
  423. }
  424. #endif
  425. /* Loading ok, update default load address */
  426. load_addr = addr;
  427. /* Check if we should attempt an auto-start */
  428. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  429. char *local_args[2];
  430. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  431. local_args[0] = argv[0];
  432. local_args[1] = NULL;
  433. printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
  434. do_bootm (cmdtp, 0, 1, local_args);
  435. rcode = 1;
  436. }
  437. return rcode;
  438. }
  439. /* ------------------------------------------------------------------------- */
  440. void inline
  441. __ide_outb(int dev, int port, unsigned char val)
  442. {
  443. debug ("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
  444. dev, port, val, (ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port)));
  445. outb(val, (ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port)));
  446. }
  447. void inline ide_outb (int dev, int port, unsigned char val)
  448. __attribute__((weak, alias("__ide_outb")));
  449. unsigned char inline
  450. __ide_inb(int dev, int port)
  451. {
  452. uchar val;
  453. val = inb((ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port)));
  454. debug ("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
  455. dev, port, (ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port)), val);
  456. return val;
  457. }
  458. unsigned char inline ide_inb(int dev, int port)
  459. __attribute__((weak, alias("__ide_inb")));
  460. void ide_init (void)
  461. {
  462. #ifdef CONFIG_IDE_8xx_DIRECT
  463. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  464. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  465. #endif
  466. unsigned char c;
  467. int i, bus;
  468. #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
  469. unsigned int ata_reset_time = ATA_RESET_TIME;
  470. char *s;
  471. #endif
  472. #ifdef CONFIG_AMIGAONEG3SE
  473. unsigned int max_bus_scan;
  474. #endif
  475. #ifdef CONFIG_IDE_8xx_PCCARD
  476. extern int pcmcia_on (void);
  477. extern int ide_devices_found; /* Initialized in check_ide_device() */
  478. #endif /* CONFIG_IDE_8xx_PCCARD */
  479. #ifdef CONFIG_IDE_PREINIT
  480. extern int ide_preinit (void);
  481. WATCHDOG_RESET();
  482. if (ide_preinit ()) {
  483. puts ("ide_preinit failed\n");
  484. return;
  485. }
  486. #endif /* CONFIG_IDE_PREINIT */
  487. #ifdef CONFIG_IDE_8xx_PCCARD
  488. extern int pcmcia_on (void);
  489. extern int ide_devices_found; /* Initialized in check_ide_device() */
  490. WATCHDOG_RESET();
  491. ide_devices_found = 0;
  492. /* initialize the PCMCIA IDE adapter card */
  493. pcmcia_on();
  494. if (!ide_devices_found)
  495. return;
  496. udelay (1000000); /* 1 s */
  497. #endif /* CONFIG_IDE_8xx_PCCARD */
  498. WATCHDOG_RESET();
  499. #ifdef CONFIG_IDE_8xx_DIRECT
  500. /* Initialize PIO timing tables */
  501. for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
  502. pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
  503. gd->bus_clk);
  504. pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
  505. gd->bus_clk);
  506. pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
  507. gd->bus_clk);
  508. debug ( "PIO Mode %d: setup=%2d ns/%d clk"
  509. " len=%3d ns/%d clk"
  510. " hold=%2d ns/%d clk\n",
  511. i,
  512. pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
  513. pio_config_ns[i].t_length, pio_config_clk[i].t_length,
  514. pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
  515. }
  516. #endif /* CONFIG_IDE_8xx_DIRECT */
  517. /* Reset the IDE just to be sure.
  518. * Light LED's to show
  519. */
  520. ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
  521. ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
  522. #ifdef CONFIG_IDE_8xx_DIRECT
  523. /* PCMCIA / IDE initialization for common mem space */
  524. pcmp->pcmc_pgcrb = 0;
  525. /* start in PIO mode 0 - most relaxed timings */
  526. pio_mode = 0;
  527. set_pcmcia_timing (pio_mode);
  528. #endif /* CONFIG_IDE_8xx_DIRECT */
  529. /*
  530. * Wait for IDE to get ready.
  531. * According to spec, this can take up to 31 seconds!
  532. */
  533. #ifndef CONFIG_AMIGAONEG3SE
  534. for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
  535. int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
  536. #else
  537. s = getenv("ide_maxbus");
  538. if (s)
  539. max_bus_scan = simple_strtol(s, NULL, 10);
  540. else
  541. max_bus_scan = CFG_IDE_MAXBUS;
  542. for (bus=0; bus<max_bus_scan; ++bus) {
  543. int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
  544. #endif
  545. #ifdef CONFIG_IDE_8xx_PCCARD
  546. /* Skip non-ide devices from probing */
  547. if ((ide_devices_found & (1 << bus)) == 0) {
  548. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  549. continue;
  550. }
  551. #endif
  552. printf ("Bus %d: ", bus);
  553. ide_bus_ok[bus] = 0;
  554. /* Select device
  555. */
  556. udelay (100000); /* 100 ms */
  557. ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  558. udelay (100000); /* 100 ms */
  559. #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
  560. if ((s = getenv("ide_reset_timeout")) != NULL)
  561. ata_reset_time = simple_strtol(s, NULL, 10);
  562. #endif
  563. i = 0;
  564. do {
  565. udelay (10000); /* 10 ms */
  566. c = ide_inb (dev, ATA_STATUS);
  567. i++;
  568. #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
  569. if (i > (ata_reset_time * 100)) {
  570. #else
  571. if (i > (ATA_RESET_TIME * 100)) {
  572. #endif
  573. puts ("** Timeout **\n");
  574. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  575. #ifdef CONFIG_AMIGAONEG3SE
  576. /* If this is the second bus, the first one was OK */
  577. if (bus != 0) {
  578. ide_bus_ok[bus] = 0;
  579. goto skip_bus;
  580. }
  581. #endif
  582. return;
  583. }
  584. if ((i >= 100) && ((i%100)==0)) {
  585. putc ('.');
  586. }
  587. } while (c & ATA_STAT_BUSY);
  588. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  589. puts ("not available ");
  590. debug ("Status = 0x%02X ", c);
  591. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  592. } else if ((c & ATA_STAT_READY) == 0) {
  593. puts ("not available ");
  594. debug ("Status = 0x%02X ", c);
  595. #endif
  596. } else {
  597. puts ("OK ");
  598. ide_bus_ok[bus] = 1;
  599. }
  600. WATCHDOG_RESET();
  601. }
  602. #ifdef CONFIG_AMIGAONEG3SE
  603. skip_bus:
  604. #endif
  605. putc ('\n');
  606. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  607. curr_device = -1;
  608. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  609. #ifdef CONFIG_IDE_LED
  610. int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
  611. #endif
  612. ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
  613. ide_dev_desc[i].if_type=IF_TYPE_IDE;
  614. ide_dev_desc[i].dev=i;
  615. ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
  616. ide_dev_desc[i].blksz=0;
  617. ide_dev_desc[i].lba=0;
  618. ide_dev_desc[i].block_read=ide_read;
  619. if (!ide_bus_ok[IDE_BUS(i)])
  620. continue;
  621. ide_led (led, 1); /* LED on */
  622. ide_ident(&ide_dev_desc[i]);
  623. ide_led (led, 0); /* LED off */
  624. dev_print(&ide_dev_desc[i]);
  625. /* ide_print (i); */
  626. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  627. init_part (&ide_dev_desc[i]); /* initialize partition type */
  628. if (curr_device < 0)
  629. curr_device = i;
  630. }
  631. }
  632. WATCHDOG_RESET();
  633. }
  634. /* ------------------------------------------------------------------------- */
  635. block_dev_desc_t * ide_get_dev(int dev)
  636. {
  637. return (dev < CFG_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
  638. }
  639. #ifdef CONFIG_IDE_8xx_DIRECT
  640. static void
  641. set_pcmcia_timing (int pmode)
  642. {
  643. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  644. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  645. ulong timings;
  646. debug ("Set timing for PIO Mode %d\n", pmode);
  647. timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
  648. | PCMCIA_SST(pio_config_clk[pmode].t_setup)
  649. | PCMCIA_SL (pio_config_clk[pmode].t_length)
  650. ;
  651. /* IDE 0
  652. */
  653. pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
  654. pcmp->pcmc_por0 = CFG_PCMCIA_POR0
  655. #if (CFG_PCMCIA_POR0 != 0)
  656. | timings
  657. #endif
  658. ;
  659. debug ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
  660. pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
  661. pcmp->pcmc_por1 = CFG_PCMCIA_POR1
  662. #if (CFG_PCMCIA_POR1 != 0)
  663. | timings
  664. #endif
  665. ;
  666. debug ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
  667. pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
  668. pcmp->pcmc_por2 = CFG_PCMCIA_POR2
  669. #if (CFG_PCMCIA_POR2 != 0)
  670. | timings
  671. #endif
  672. ;
  673. debug ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
  674. pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
  675. pcmp->pcmc_por3 = CFG_PCMCIA_POR3
  676. #if (CFG_PCMCIA_POR3 != 0)
  677. | timings
  678. #endif
  679. ;
  680. debug ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
  681. /* IDE 1
  682. */
  683. pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
  684. pcmp->pcmc_por4 = CFG_PCMCIA_POR4
  685. #if (CFG_PCMCIA_POR4 != 0)
  686. | timings
  687. #endif
  688. ;
  689. debug ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
  690. pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
  691. pcmp->pcmc_por5 = CFG_PCMCIA_POR5
  692. #if (CFG_PCMCIA_POR5 != 0)
  693. | timings
  694. #endif
  695. ;
  696. debug ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
  697. pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
  698. pcmp->pcmc_por6 = CFG_PCMCIA_POR6
  699. #if (CFG_PCMCIA_POR6 != 0)
  700. | timings
  701. #endif
  702. ;
  703. debug ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
  704. pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
  705. pcmp->pcmc_por7 = CFG_PCMCIA_POR7
  706. #if (CFG_PCMCIA_POR7 != 0)
  707. | timings
  708. #endif
  709. ;
  710. debug ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
  711. }
  712. #endif /* CONFIG_IDE_8xx_DIRECT */
  713. /* ------------------------------------------------------------------------- */
  714. #ifdef __PPC__
  715. # ifdef CONFIG_AMIGAONEG3SE
  716. static void
  717. output_data_short(int dev, ulong *sect_buf, int words)
  718. {
  719. ushort *dbuf;
  720. volatile ushort *pbuf;
  721. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  722. dbuf = (ushort *)sect_buf;
  723. while (words--) {
  724. EIEIO;
  725. *pbuf = *dbuf++;
  726. EIEIO;
  727. }
  728. if (words&1)
  729. *pbuf = 0;
  730. }
  731. # endif /* CONFIG_AMIGAONEG3SE */
  732. #endif /* __PPC_ */
  733. /* We only need to swap data if we are running on a big endian cpu. */
  734. /* But Au1x00 cpu:s already swaps data in big endian mode! */
  735. #if defined(__LITTLE_ENDIAN) || ( defined(CONFIG_AU1X00) && !defined(CONFIG_GTH2) )
  736. #define input_swap_data(x,y,z) input_data(x,y,z)
  737. #else
  738. static void
  739. input_swap_data(int dev, ulong *sect_buf, int words)
  740. {
  741. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  742. uchar i;
  743. volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  744. volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  745. ushort *dbuf = (ushort *)sect_buf;
  746. while (words--) {
  747. for (i=0; i<2; i++) {
  748. *(((uchar *)(dbuf)) + 1) = *pbuf_even;
  749. *(uchar *)dbuf = *pbuf_odd;
  750. dbuf+=1;
  751. }
  752. }
  753. #else
  754. volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  755. ushort *dbuf = (ushort *)sect_buf;
  756. debug("in input swap data base for read is %lx\n", (unsigned long) pbuf);
  757. while (words--) {
  758. #ifdef __MIPS__
  759. *dbuf++ = swab16p((u16*)pbuf);
  760. *dbuf++ = swab16p((u16*)pbuf);
  761. #elif defined(CONFIG_PCS440EP)
  762. *dbuf++ = *pbuf;
  763. *dbuf++ = *pbuf;
  764. #else
  765. *dbuf++ = ld_le16(pbuf);
  766. *dbuf++ = ld_le16(pbuf);
  767. #endif /* !MIPS */
  768. }
  769. #endif
  770. }
  771. #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
  772. #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA) || defined(CONFIG_SH)
  773. static void
  774. output_data(int dev, ulong *sect_buf, int words)
  775. {
  776. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  777. uchar *dbuf;
  778. volatile uchar *pbuf_even;
  779. volatile uchar *pbuf_odd;
  780. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  781. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  782. dbuf = (uchar *)sect_buf;
  783. while (words--) {
  784. EIEIO;
  785. *pbuf_even = *dbuf++;
  786. EIEIO;
  787. *pbuf_odd = *dbuf++;
  788. EIEIO;
  789. *pbuf_even = *dbuf++;
  790. EIEIO;
  791. *pbuf_odd = *dbuf++;
  792. }
  793. #else
  794. ushort *dbuf;
  795. volatile ushort *pbuf;
  796. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  797. dbuf = (ushort *)sect_buf;
  798. while (words--) {
  799. #if defined(CONFIG_PCS440EP)
  800. /* not tested, because CF was write protected */
  801. EIEIO;
  802. *pbuf = ld_le16(dbuf++);
  803. EIEIO;
  804. *pbuf = ld_le16(dbuf++);
  805. #else
  806. EIEIO;
  807. *pbuf = *dbuf++;
  808. EIEIO;
  809. *pbuf = *dbuf++;
  810. #endif
  811. }
  812. #endif
  813. }
  814. #else /* ! __PPC__ */
  815. static void
  816. output_data(int dev, ulong *sect_buf, int words)
  817. {
  818. outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
  819. }
  820. #endif /* __PPC__ */
  821. #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA) || defined(CONFIG_SH)
  822. static void
  823. input_data(int dev, ulong *sect_buf, int words)
  824. {
  825. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  826. uchar *dbuf;
  827. volatile uchar *pbuf_even;
  828. volatile uchar *pbuf_odd;
  829. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  830. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  831. dbuf = (uchar *)sect_buf;
  832. while (words--) {
  833. *dbuf++ = *pbuf_even;
  834. EIEIO;
  835. SYNC;
  836. *dbuf++ = *pbuf_odd;
  837. EIEIO;
  838. SYNC;
  839. *dbuf++ = *pbuf_even;
  840. EIEIO;
  841. SYNC;
  842. *dbuf++ = *pbuf_odd;
  843. EIEIO;
  844. SYNC;
  845. }
  846. #else
  847. ushort *dbuf;
  848. volatile ushort *pbuf;
  849. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  850. dbuf = (ushort *)sect_buf;
  851. debug("in input data base for read is %lx\n", (unsigned long) pbuf);
  852. while (words--) {
  853. #if defined(CONFIG_PCS440EP)
  854. EIEIO;
  855. *dbuf++ = ld_le16(pbuf);
  856. EIEIO;
  857. *dbuf++ = ld_le16(pbuf);
  858. #else
  859. EIEIO;
  860. *dbuf++ = *pbuf;
  861. EIEIO;
  862. *dbuf++ = *pbuf;
  863. #endif
  864. }
  865. #endif
  866. }
  867. #else /* ! __PPC__ */
  868. static void
  869. input_data(int dev, ulong *sect_buf, int words)
  870. {
  871. insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
  872. }
  873. #endif /* __PPC__ */
  874. #ifdef CONFIG_AMIGAONEG3SE
  875. static void
  876. input_data_short(int dev, ulong *sect_buf, int words)
  877. {
  878. ushort *dbuf;
  879. volatile ushort *pbuf;
  880. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  881. dbuf = (ushort *)sect_buf;
  882. while (words--) {
  883. EIEIO;
  884. *dbuf++ = *pbuf;
  885. EIEIO;
  886. }
  887. if (words&1) {
  888. ushort dummy;
  889. dummy = *pbuf;
  890. }
  891. }
  892. #endif
  893. /* -------------------------------------------------------------------------
  894. */
  895. static void ide_ident (block_dev_desc_t *dev_desc)
  896. {
  897. ulong iobuf[ATA_SECTORWORDS];
  898. unsigned char c;
  899. hd_driveid_t *iop = (hd_driveid_t *)iobuf;
  900. #ifdef CONFIG_AMIGAONEG3SE
  901. int max_bus_scan;
  902. char *s;
  903. #endif
  904. #ifdef CONFIG_ATAPI
  905. int retries = 0;
  906. int do_retry = 0;
  907. #endif
  908. #if 0
  909. int mode, cycle_time;
  910. #endif
  911. int device;
  912. device=dev_desc->dev;
  913. printf (" Device %d: ", device);
  914. #ifdef CONFIG_AMIGAONEG3SE
  915. s = getenv("ide_maxbus");
  916. if (s) {
  917. max_bus_scan = simple_strtol(s, NULL, 10);
  918. } else {
  919. max_bus_scan = CFG_IDE_MAXBUS;
  920. }
  921. if (device >= max_bus_scan*2) {
  922. dev_desc->type=DEV_TYPE_UNKNOWN;
  923. return;
  924. }
  925. #endif
  926. ide_led (DEVICE_LED(device), 1); /* LED on */
  927. /* Select device
  928. */
  929. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  930. dev_desc->if_type=IF_TYPE_IDE;
  931. #ifdef CONFIG_ATAPI
  932. do_retry = 0;
  933. retries = 0;
  934. /* Warning: This will be tricky to read */
  935. while (retries <= 1) {
  936. /* check signature */
  937. if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
  938. (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
  939. (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
  940. (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
  941. /* ATAPI Signature found */
  942. dev_desc->if_type=IF_TYPE_ATAPI;
  943. /* Start Ident Command
  944. */
  945. ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
  946. /*
  947. * Wait for completion - ATAPI devices need more time
  948. * to become ready
  949. */
  950. c = ide_wait (device, ATAPI_TIME_OUT);
  951. } else
  952. #endif
  953. {
  954. /* Start Ident Command
  955. */
  956. ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
  957. /* Wait for completion
  958. */
  959. c = ide_wait (device, IDE_TIME_OUT);
  960. }
  961. ide_led (DEVICE_LED(device), 0); /* LED off */
  962. if (((c & ATA_STAT_DRQ) == 0) ||
  963. ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
  964. #ifdef CONFIG_ATAPI
  965. #ifdef CONFIG_AMIGAONEG3SE
  966. s = getenv("ide_doreset");
  967. if (s && strcmp(s, "on") == 0)
  968. #endif
  969. {
  970. /* Need to soft reset the device in case it's an ATAPI... */
  971. debug ("Retrying...\n");
  972. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  973. udelay(100000);
  974. ide_outb (device, ATA_COMMAND, 0x08);
  975. udelay (500000); /* 500 ms */
  976. }
  977. /* Select device
  978. */
  979. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  980. retries++;
  981. #else
  982. return;
  983. #endif
  984. }
  985. #ifdef CONFIG_ATAPI
  986. else
  987. break;
  988. } /* see above - ugly to read */
  989. if (retries == 2) /* Not found */
  990. return;
  991. #endif
  992. input_swap_data (device, iobuf, ATA_SECTORWORDS);
  993. ident_cpy ((unsigned char*)dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
  994. ident_cpy ((unsigned char*)dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
  995. ident_cpy ((unsigned char*)dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
  996. #ifdef __LITTLE_ENDIAN
  997. /*
  998. * firmware revision and model number have Big Endian Byte
  999. * order in Word. Convert both to little endian.
  1000. *
  1001. * See CF+ and CompactFlash Specification Revision 2.0:
  1002. * 6.2.1.6: Identfy Drive, Table 39 for more details
  1003. */
  1004. strswab (dev_desc->revision);
  1005. strswab (dev_desc->vendor);
  1006. #endif /* __LITTLE_ENDIAN */
  1007. if ((iop->config & 0x0080)==0x0080)
  1008. dev_desc->removable = 1;
  1009. else
  1010. dev_desc->removable = 0;
  1011. #if 0
  1012. /*
  1013. * Drive PIO mode autoselection
  1014. */
  1015. mode = iop->tPIO;
  1016. printf ("tPIO = 0x%02x = %d\n",mode, mode);
  1017. if (mode > 2) { /* 2 is maximum allowed tPIO value */
  1018. mode = 2;
  1019. debug ("Override tPIO -> 2\n");
  1020. }
  1021. if (iop->field_valid & 2) { /* drive implements ATA2? */
  1022. debug ("Drive implements ATA2\n");
  1023. if (iop->capability & 8) { /* drive supports use_iordy? */
  1024. cycle_time = iop->eide_pio_iordy;
  1025. } else {
  1026. cycle_time = iop->eide_pio;
  1027. }
  1028. debug ("cycle time = %d\n", cycle_time);
  1029. mode = 4;
  1030. if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
  1031. if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
  1032. if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
  1033. if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
  1034. }
  1035. printf ("PIO mode to use: PIO %d\n", mode);
  1036. #endif /* 0 */
  1037. #ifdef CONFIG_ATAPI
  1038. if (dev_desc->if_type==IF_TYPE_ATAPI) {
  1039. atapi_inquiry(dev_desc);
  1040. return;
  1041. }
  1042. #endif /* CONFIG_ATAPI */
  1043. #ifdef __BIG_ENDIAN
  1044. /* swap shorts */
  1045. dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
  1046. #else /* ! __BIG_ENDIAN */
  1047. /*
  1048. * do not swap shorts on little endian
  1049. *
  1050. * See CF+ and CompactFlash Specification Revision 2.0:
  1051. * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
  1052. */
  1053. dev_desc->lba = iop->lba_capacity;
  1054. #endif /* __BIG_ENDIAN */
  1055. #ifdef CONFIG_LBA48
  1056. if (iop->command_set_2 & 0x0400) { /* LBA 48 support */
  1057. dev_desc->lba48 = 1;
  1058. dev_desc->lba = (unsigned long long)iop->lba48_capacity[0] |
  1059. ((unsigned long long)iop->lba48_capacity[1] << 16) |
  1060. ((unsigned long long)iop->lba48_capacity[2] << 32) |
  1061. ((unsigned long long)iop->lba48_capacity[3] << 48);
  1062. } else {
  1063. dev_desc->lba48 = 0;
  1064. }
  1065. #endif /* CONFIG_LBA48 */
  1066. /* assuming HD */
  1067. dev_desc->type=DEV_TYPE_HARDDISK;
  1068. dev_desc->blksz=ATA_BLOCKSIZE;
  1069. dev_desc->lun=0; /* just to fill something in... */
  1070. #if 0 /* only used to test the powersaving mode,
  1071. * if enabled, the drive goes after 5 sec
  1072. * in standby mode */
  1073. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1074. c = ide_wait (device, IDE_TIME_OUT);
  1075. ide_outb (device, ATA_SECT_CNT, 1);
  1076. ide_outb (device, ATA_LBA_LOW, 0);
  1077. ide_outb (device, ATA_LBA_MID, 0);
  1078. ide_outb (device, ATA_LBA_HIGH, 0);
  1079. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1080. ide_outb (device, ATA_COMMAND, 0xe3);
  1081. udelay (50);
  1082. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1083. #endif
  1084. }
  1085. /* ------------------------------------------------------------------------- */
  1086. ulong ide_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  1087. {
  1088. ulong n = 0;
  1089. unsigned char c;
  1090. unsigned char pwrsave=0; /* power save */
  1091. #ifdef CONFIG_LBA48
  1092. unsigned char lba48 = 0;
  1093. if (blknr & 0x0000fffff0000000ULL) {
  1094. /* more than 28 bits used, use 48bit mode */
  1095. lba48 = 1;
  1096. }
  1097. #endif
  1098. debug ("ide_read dev %d start %qX, blocks %lX buffer at %lX\n",
  1099. device, blknr, blkcnt, (ulong)buffer);
  1100. ide_led (DEVICE_LED(device), 1); /* LED on */
  1101. /* Select device
  1102. */
  1103. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1104. c = ide_wait (device, IDE_TIME_OUT);
  1105. if (c & ATA_STAT_BUSY) {
  1106. printf ("IDE read: device %d not ready\n", device);
  1107. goto IDE_READ_E;
  1108. }
  1109. /* first check if the drive is in Powersaving mode, if yes,
  1110. * increase the timeout value */
  1111. ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
  1112. udelay (50);
  1113. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1114. if (c & ATA_STAT_BUSY) {
  1115. printf ("IDE read: device %d not ready\n", device);
  1116. goto IDE_READ_E;
  1117. }
  1118. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1119. printf ("No Powersaving mode %X\n", c);
  1120. } else {
  1121. c = ide_inb(device,ATA_SECT_CNT);
  1122. debug ("Powersaving %02X\n",c);
  1123. if(c==0)
  1124. pwrsave=1;
  1125. }
  1126. while (blkcnt-- > 0) {
  1127. c = ide_wait (device, IDE_TIME_OUT);
  1128. if (c & ATA_STAT_BUSY) {
  1129. printf ("IDE read: device %d not ready\n", device);
  1130. break;
  1131. }
  1132. #ifdef CONFIG_LBA48
  1133. if (lba48) {
  1134. /* write high bits */
  1135. ide_outb (device, ATA_SECT_CNT, 0);
  1136. ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  1137. #ifdef CFG_64BIT_LBA
  1138. ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  1139. ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  1140. #else
  1141. ide_outb (device, ATA_LBA_MID, 0);
  1142. ide_outb (device, ATA_LBA_HIGH, 0);
  1143. #endif
  1144. }
  1145. #endif
  1146. ide_outb (device, ATA_SECT_CNT, 1);
  1147. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  1148. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  1149. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  1150. #ifdef CONFIG_LBA48
  1151. if (lba48) {
  1152. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
  1153. ide_outb (device, ATA_COMMAND, ATA_CMD_READ_EXT);
  1154. } else
  1155. #endif
  1156. {
  1157. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  1158. ATA_DEVICE(device) |
  1159. ((blknr >> 24) & 0xF) );
  1160. ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
  1161. }
  1162. udelay (50);
  1163. if(pwrsave) {
  1164. c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
  1165. pwrsave=0;
  1166. } else {
  1167. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1168. }
  1169. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  1170. #if defined(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
  1171. printf ("Error (no IRQ) dev %d blk %qd: status 0x%02x\n",
  1172. device, blknr, c);
  1173. #else
  1174. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  1175. device, (ulong)blknr, c);
  1176. #endif
  1177. break;
  1178. }
  1179. input_data (device, buffer, ATA_SECTORWORDS);
  1180. (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
  1181. ++n;
  1182. ++blknr;
  1183. buffer += ATA_BLOCKSIZE;
  1184. }
  1185. IDE_READ_E:
  1186. ide_led (DEVICE_LED(device), 0); /* LED off */
  1187. return (n);
  1188. }
  1189. /* ------------------------------------------------------------------------- */
  1190. ulong ide_write (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  1191. {
  1192. ulong n = 0;
  1193. unsigned char c;
  1194. #ifdef CONFIG_LBA48
  1195. unsigned char lba48 = 0;
  1196. if (blknr & 0x0000fffff0000000ULL) {
  1197. /* more than 28 bits used, use 48bit mode */
  1198. lba48 = 1;
  1199. }
  1200. #endif
  1201. ide_led (DEVICE_LED(device), 1); /* LED on */
  1202. /* Select device
  1203. */
  1204. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1205. while (blkcnt-- > 0) {
  1206. c = ide_wait (device, IDE_TIME_OUT);
  1207. if (c & ATA_STAT_BUSY) {
  1208. printf ("IDE read: device %d not ready\n", device);
  1209. goto WR_OUT;
  1210. }
  1211. #ifdef CONFIG_LBA48
  1212. if (lba48) {
  1213. /* write high bits */
  1214. ide_outb (device, ATA_SECT_CNT, 0);
  1215. ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  1216. #ifdef CFG_64BIT_LBA
  1217. ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  1218. ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  1219. #else
  1220. ide_outb (device, ATA_LBA_MID, 0);
  1221. ide_outb (device, ATA_LBA_HIGH, 0);
  1222. #endif
  1223. }
  1224. #endif
  1225. ide_outb (device, ATA_SECT_CNT, 1);
  1226. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  1227. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  1228. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  1229. #ifdef CONFIG_LBA48
  1230. if (lba48) {
  1231. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
  1232. ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
  1233. } else
  1234. #endif
  1235. {
  1236. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  1237. ATA_DEVICE(device) |
  1238. ((blknr >> 24) & 0xF) );
  1239. ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
  1240. }
  1241. udelay (50);
  1242. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1243. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  1244. #if defined(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
  1245. printf ("Error (no IRQ) dev %d blk %qd: status 0x%02x\n",
  1246. device, blknr, c);
  1247. #else
  1248. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  1249. device, (ulong)blknr, c);
  1250. #endif
  1251. goto WR_OUT;
  1252. }
  1253. output_data (device, buffer, ATA_SECTORWORDS);
  1254. c = ide_inb (device, ATA_STATUS); /* clear IRQ */
  1255. ++n;
  1256. ++blknr;
  1257. buffer += ATA_BLOCKSIZE;
  1258. }
  1259. WR_OUT:
  1260. ide_led (DEVICE_LED(device), 0); /* LED off */
  1261. return (n);
  1262. }
  1263. /* ------------------------------------------------------------------------- */
  1264. /*
  1265. * copy src to dest, skipping leading and trailing blanks and null
  1266. * terminate the string
  1267. * "len" is the size of available memory including the terminating '\0'
  1268. */
  1269. static void ident_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
  1270. {
  1271. unsigned char *end, *last;
  1272. last = dst;
  1273. end = src + len - 1;
  1274. /* reserve space for '\0' */
  1275. if (len < 2)
  1276. goto OUT;
  1277. /* skip leading white space */
  1278. while ((*src) && (src<end) && (*src==' '))
  1279. ++src;
  1280. /* copy string, omitting trailing white space */
  1281. while ((*src) && (src<end)) {
  1282. *dst++ = *src;
  1283. if (*src++ != ' ')
  1284. last = dst;
  1285. }
  1286. OUT:
  1287. *last = '\0';
  1288. }
  1289. /* ------------------------------------------------------------------------- */
  1290. /*
  1291. * Wait until Busy bit is off, or timeout (in ms)
  1292. * Return last status
  1293. */
  1294. static uchar ide_wait (int dev, ulong t)
  1295. {
  1296. ulong delay = 10 * t; /* poll every 100 us */
  1297. uchar c;
  1298. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  1299. udelay (100);
  1300. if (delay-- == 0) {
  1301. break;
  1302. }
  1303. }
  1304. return (c);
  1305. }
  1306. /* ------------------------------------------------------------------------- */
  1307. #ifdef CONFIG_IDE_RESET
  1308. extern void ide_set_reset(int idereset);
  1309. static void ide_reset (void)
  1310. {
  1311. #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
  1312. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  1313. #endif
  1314. int i;
  1315. curr_device = -1;
  1316. for (i=0; i<CFG_IDE_MAXBUS; ++i)
  1317. ide_bus_ok[i] = 0;
  1318. for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
  1319. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  1320. ide_set_reset (1); /* assert reset */
  1321. /* the reset signal shall be asserted for et least 25 us */
  1322. udelay(25);
  1323. WATCHDOG_RESET();
  1324. #ifdef CFG_PB_12V_ENABLE
  1325. immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
  1326. immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
  1327. immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
  1328. immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
  1329. /* wait 500 ms for the voltage to stabilize
  1330. */
  1331. for (i=0; i<500; ++i) {
  1332. udelay (1000);
  1333. }
  1334. immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
  1335. #endif /* CFG_PB_12V_ENABLE */
  1336. #ifdef CFG_PB_IDE_MOTOR
  1337. /* configure IDE Motor voltage monitor pin as input */
  1338. immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
  1339. immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
  1340. immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
  1341. /* wait up to 1 s for the motor voltage to stabilize
  1342. */
  1343. for (i=0; i<1000; ++i) {
  1344. if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
  1345. break;
  1346. }
  1347. udelay (1000);
  1348. }
  1349. if (i == 1000) { /* Timeout */
  1350. printf ("\nWarning: 5V for IDE Motor missing\n");
  1351. # ifdef CONFIG_STATUS_LED
  1352. # ifdef STATUS_LED_YELLOW
  1353. status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
  1354. # endif
  1355. # ifdef STATUS_LED_GREEN
  1356. status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
  1357. # endif
  1358. # endif /* CONFIG_STATUS_LED */
  1359. }
  1360. #endif /* CFG_PB_IDE_MOTOR */
  1361. WATCHDOG_RESET();
  1362. /* de-assert RESET signal */
  1363. ide_set_reset(0);
  1364. /* wait 250 ms */
  1365. for (i=0; i<250; ++i) {
  1366. udelay (1000);
  1367. }
  1368. }
  1369. #endif /* CONFIG_IDE_RESET */
  1370. /* ------------------------------------------------------------------------- */
  1371. #if defined(CONFIG_IDE_LED) && \
  1372. !defined(CONFIG_AMIGAONEG3SE)&& \
  1373. !defined(CONFIG_CPC45) && \
  1374. !defined(CONFIG_HMI10) && \
  1375. !defined(CONFIG_KUP4K) && \
  1376. !defined(CONFIG_KUP4X)
  1377. static uchar led_buffer = 0; /* Buffer for current LED status */
  1378. static void ide_led (uchar led, uchar status)
  1379. {
  1380. uchar *led_port = LED_PORT;
  1381. if (status) { /* switch LED on */
  1382. led_buffer |= led;
  1383. } else { /* switch LED off */
  1384. led_buffer &= ~led;
  1385. }
  1386. *led_port = led_buffer;
  1387. }
  1388. #endif /* CONFIG_IDE_LED */
  1389. /* ------------------------------------------------------------------------- */
  1390. #ifdef CONFIG_ATAPI
  1391. /****************************************************************************
  1392. * ATAPI Support
  1393. */
  1394. #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA)
  1395. /* since ATAPI may use commands with not 4 bytes alligned length
  1396. * we have our own transfer functions, 2 bytes alligned */
  1397. static void
  1398. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1399. {
  1400. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  1401. uchar *dbuf;
  1402. volatile uchar *pbuf_even;
  1403. volatile uchar *pbuf_odd;
  1404. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  1405. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  1406. while (shorts--) {
  1407. EIEIO;
  1408. *pbuf_even = *dbuf++;
  1409. EIEIO;
  1410. *pbuf_odd = *dbuf++;
  1411. }
  1412. #else
  1413. ushort *dbuf;
  1414. volatile ushort *pbuf;
  1415. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1416. dbuf = (ushort *)sect_buf;
  1417. debug ("in output data shorts base for read is %lx\n", (unsigned long) pbuf);
  1418. while (shorts--) {
  1419. EIEIO;
  1420. *pbuf = *dbuf++;
  1421. }
  1422. #endif
  1423. }
  1424. static void
  1425. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1426. {
  1427. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  1428. uchar *dbuf;
  1429. volatile uchar *pbuf_even;
  1430. volatile uchar *pbuf_odd;
  1431. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  1432. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  1433. while (shorts--) {
  1434. EIEIO;
  1435. *dbuf++ = *pbuf_even;
  1436. EIEIO;
  1437. *dbuf++ = *pbuf_odd;
  1438. }
  1439. #else
  1440. ushort *dbuf;
  1441. volatile ushort *pbuf;
  1442. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1443. dbuf = (ushort *)sect_buf;
  1444. debug("in input data shorts base for read is %lx\n", (unsigned long) pbuf);
  1445. while (shorts--) {
  1446. EIEIO;
  1447. *dbuf++ = *pbuf;
  1448. }
  1449. #endif
  1450. }
  1451. #else /* ! __PPC__ */
  1452. static void
  1453. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1454. {
  1455. outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
  1456. }
  1457. static void
  1458. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1459. {
  1460. insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
  1461. }
  1462. #endif /* __PPC__ */
  1463. /*
  1464. * Wait until (Status & mask) == res, or timeout (in ms)
  1465. * Return last status
  1466. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  1467. * and then they set their DRQ Bit
  1468. */
  1469. static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
  1470. {
  1471. ulong delay = 10 * t; /* poll every 100 us */
  1472. uchar c;
  1473. c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
  1474. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  1475. /* break if error occurs (doesn't make sense to wait more) */
  1476. if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
  1477. break;
  1478. udelay (100);
  1479. if (delay-- == 0) {
  1480. break;
  1481. }
  1482. }
  1483. return (c);
  1484. }
  1485. /*
  1486. * issue an atapi command
  1487. */
  1488. unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
  1489. {
  1490. unsigned char c,err,mask,res;
  1491. int n;
  1492. ide_led (DEVICE_LED(device), 1); /* LED on */
  1493. /* Select device
  1494. */
  1495. mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
  1496. res = 0;
  1497. #ifdef CONFIG_AMIGAONEG3SE
  1498. # warning THF: Removed LBA mode ???
  1499. #endif
  1500. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1501. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1502. if ((c & mask) != res) {
  1503. printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
  1504. err=0xFF;
  1505. goto AI_OUT;
  1506. }
  1507. /* write taskfile */
  1508. ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  1509. ide_outb (device, ATA_SECT_CNT, 0);
  1510. ide_outb (device, ATA_SECT_NUM, 0);
  1511. ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
  1512. ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
  1513. #ifdef CONFIG_AMIGAONEG3SE
  1514. # warning THF: Removed LBA mode ???
  1515. #endif
  1516. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1517. ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
  1518. udelay (50);
  1519. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1520. res = ATA_STAT_DRQ;
  1521. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1522. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  1523. printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
  1524. err=0xFF;
  1525. goto AI_OUT;
  1526. }
  1527. output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
  1528. /* ATAPI Command written wait for completition */
  1529. udelay (5000); /* device must set bsy */
  1530. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1531. /* if no data wait for DRQ = 0 BSY = 0
  1532. * if data wait for DRQ = 1 BSY = 0 */
  1533. res=0;
  1534. if(buflen)
  1535. res = ATA_STAT_DRQ;
  1536. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1537. if ((c & mask) != res ) {
  1538. if (c & ATA_STAT_ERR) {
  1539. err=(ide_inb(device,ATA_ERROR_REG))>>4;
  1540. debug ("atapi_issue 1 returned sense key %X status %02X\n",err,c);
  1541. } else {
  1542. printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
  1543. err=0xFF;
  1544. }
  1545. goto AI_OUT;
  1546. }
  1547. n=ide_inb(device, ATA_CYL_HIGH);
  1548. n<<=8;
  1549. n+=ide_inb(device, ATA_CYL_LOW);
  1550. if(n>buflen) {
  1551. printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
  1552. err=0xff;
  1553. goto AI_OUT;
  1554. }
  1555. if((n==0)&&(buflen<0)) {
  1556. printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
  1557. err=0xff;
  1558. goto AI_OUT;
  1559. }
  1560. if(n!=buflen) {
  1561. debug ("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
  1562. }
  1563. if(n!=0) { /* data transfer */
  1564. debug ("ATAPI_ISSUE: %d Bytes to transfer\n",n);
  1565. /* we transfer shorts */
  1566. n>>=1;
  1567. /* ok now decide if it is an in or output */
  1568. if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
  1569. debug ("Write to device\n");
  1570. output_data_shorts(device,(unsigned short *)buffer,n);
  1571. } else {
  1572. debug ("Read from device @ %p shorts %d\n",buffer,n);
  1573. input_data_shorts(device,(unsigned short *)buffer,n);
  1574. }
  1575. }
  1576. udelay(5000); /* seems that some CD ROMs need this... */
  1577. mask = ATA_STAT_BUSY|ATA_STAT_ERR;
  1578. res=0;
  1579. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1580. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1581. err=(ide_inb(device,ATA_ERROR_REG) >> 4);
  1582. debug ("atapi_issue 2 returned sense key %X status %X\n",err,c);
  1583. } else {
  1584. err = 0;
  1585. }
  1586. AI_OUT:
  1587. ide_led (DEVICE_LED(device), 0); /* LED off */
  1588. return (err);
  1589. }
  1590. /*
  1591. * sending the command to atapi_issue. If an status other than good
  1592. * returns, an request_sense will be issued
  1593. */
  1594. #define ATAPI_DRIVE_NOT_READY 100
  1595. #define ATAPI_UNIT_ATTN 10
  1596. unsigned char atapi_issue_autoreq (int device,
  1597. unsigned char* ccb,
  1598. int ccblen,
  1599. unsigned char *buffer,
  1600. int buflen)
  1601. {
  1602. unsigned char sense_data[18],sense_ccb[12];
  1603. unsigned char res,key,asc,ascq;
  1604. int notready,unitattn;
  1605. #ifdef CONFIG_AMIGAONEG3SE
  1606. char *s;
  1607. unsigned int timeout, retrycnt;
  1608. s = getenv("ide_cd_timeout");
  1609. timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
  1610. retrycnt = 0;
  1611. #endif
  1612. unitattn=ATAPI_UNIT_ATTN;
  1613. notready=ATAPI_DRIVE_NOT_READY;
  1614. retry:
  1615. res= atapi_issue(device,ccb,ccblen,buffer,buflen);
  1616. if (res==0)
  1617. return (0); /* Ok */
  1618. if (res==0xFF)
  1619. return (0xFF); /* error */
  1620. debug ("(auto_req)atapi_issue returned sense key %X\n",res);
  1621. memset(sense_ccb,0,sizeof(sense_ccb));
  1622. memset(sense_data,0,sizeof(sense_data));
  1623. sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
  1624. sense_ccb[4]=18; /* allocation Length */
  1625. res=atapi_issue(device,sense_ccb,12,sense_data,18);
  1626. key=(sense_data[2]&0xF);
  1627. asc=(sense_data[12]);
  1628. ascq=(sense_data[13]);
  1629. debug ("ATAPI_CMD_REQ_SENSE returned %x\n",res);
  1630. debug (" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1631. sense_data[0],
  1632. key,
  1633. asc,
  1634. ascq);
  1635. if((key==0))
  1636. return 0; /* ok device ready */
  1637. if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
  1638. if(unitattn-->0) {
  1639. udelay(200*1000);
  1640. goto retry;
  1641. }
  1642. printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
  1643. goto error;
  1644. }
  1645. if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
  1646. if (notready-->0) {
  1647. udelay(200*1000);
  1648. goto retry;
  1649. }
  1650. printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
  1651. goto error;
  1652. }
  1653. if(asc==0x3a) {
  1654. debug ("Media not present\n");
  1655. goto error;
  1656. }
  1657. #ifdef CONFIG_AMIGAONEG3SE
  1658. if ((sense_data[2]&0xF)==0x0B) {
  1659. debug ("ABORTED COMMAND...retry\n");
  1660. if (retrycnt++ < 4)
  1661. goto retry;
  1662. return (0xFF);
  1663. }
  1664. if ((sense_data[2]&0xf) == 0x02 &&
  1665. sense_data[12] == 0x04 &&
  1666. sense_data[13] == 0x01 ) {
  1667. debug ("Waiting for unit to become active\n");
  1668. udelay(timeout);
  1669. if (retrycnt++ < 4)
  1670. goto retry;
  1671. return 0xFF;
  1672. }
  1673. #endif /* CONFIG_AMIGAONEG3SE */
  1674. printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1675. error:
  1676. debug ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1677. return (0xFF);
  1678. }
  1679. static void atapi_inquiry(block_dev_desc_t * dev_desc)
  1680. {
  1681. unsigned char ccb[12]; /* Command descriptor block */
  1682. unsigned char iobuf[64]; /* temp buf */
  1683. unsigned char c;
  1684. int device;
  1685. device=dev_desc->dev;
  1686. dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
  1687. dev_desc->block_read=atapi_read;
  1688. memset(ccb,0,sizeof(ccb));
  1689. memset(iobuf,0,sizeof(iobuf));
  1690. ccb[0]=ATAPI_CMD_INQUIRY;
  1691. ccb[4]=40; /* allocation Legnth */
  1692. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
  1693. debug ("ATAPI_CMD_INQUIRY returned %x\n",c);
  1694. if (c!=0)
  1695. return;
  1696. /* copy device ident strings */
  1697. ident_cpy((unsigned char*)dev_desc->vendor,&iobuf[8],8);
  1698. ident_cpy((unsigned char*)dev_desc->product,&iobuf[16],16);
  1699. ident_cpy((unsigned char*)dev_desc->revision,&iobuf[32],5);
  1700. dev_desc->lun=0;
  1701. dev_desc->lba=0;
  1702. dev_desc->blksz=0;
  1703. dev_desc->type=iobuf[0] & 0x1f;
  1704. if ((iobuf[1]&0x80)==0x80)
  1705. dev_desc->removable = 1;
  1706. else
  1707. dev_desc->removable = 0;
  1708. memset(ccb,0,sizeof(ccb));
  1709. memset(iobuf,0,sizeof(iobuf));
  1710. ccb[0]=ATAPI_CMD_START_STOP;
  1711. ccb[4]=0x03; /* start */
  1712. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1713. debug ("ATAPI_CMD_START_STOP returned %x\n",c);
  1714. if (c!=0)
  1715. return;
  1716. memset(ccb,0,sizeof(ccb));
  1717. memset(iobuf,0,sizeof(iobuf));
  1718. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1719. debug ("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
  1720. if (c!=0)
  1721. return;
  1722. memset(ccb,0,sizeof(ccb));
  1723. memset(iobuf,0,sizeof(iobuf));
  1724. ccb[0]=ATAPI_CMD_READ_CAP;
  1725. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
  1726. debug ("ATAPI_CMD_READ_CAP returned %x\n",c);
  1727. if (c!=0)
  1728. return;
  1729. debug ("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1730. iobuf[0],iobuf[1],iobuf[2],iobuf[3],
  1731. iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
  1732. dev_desc->lba =((unsigned long)iobuf[0]<<24) +
  1733. ((unsigned long)iobuf[1]<<16) +
  1734. ((unsigned long)iobuf[2]<< 8) +
  1735. ((unsigned long)iobuf[3]);
  1736. dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
  1737. ((unsigned long)iobuf[5]<<16) +
  1738. ((unsigned long)iobuf[6]<< 8) +
  1739. ((unsigned long)iobuf[7]);
  1740. #ifdef CONFIG_LBA48
  1741. dev_desc->lba48 = 0; /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
  1742. #endif
  1743. return;
  1744. }
  1745. /*
  1746. * atapi_read:
  1747. * we transfer only one block per command, since the multiple DRQ per
  1748. * command is not yet implemented
  1749. */
  1750. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1751. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1752. #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
  1753. ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  1754. {
  1755. ulong n = 0;
  1756. unsigned char ccb[12]; /* Command descriptor block */
  1757. ulong cnt;
  1758. debug ("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1759. device, blknr, blkcnt, (ulong)buffer);
  1760. do {
  1761. if (blkcnt>ATAPI_READ_MAX_BLOCK) {
  1762. cnt=ATAPI_READ_MAX_BLOCK;
  1763. } else {
  1764. cnt=blkcnt;
  1765. }
  1766. ccb[0]=ATAPI_CMD_READ_12;
  1767. ccb[1]=0; /* reserved */
  1768. ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
  1769. ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
  1770. ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
  1771. ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
  1772. ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
  1773. ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
  1774. ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
  1775. ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
  1776. ccb[10]=0; /* reserved */
  1777. ccb[11]=0; /* reserved */
  1778. if (atapi_issue_autoreq(device,ccb,12,
  1779. (unsigned char *)buffer,
  1780. cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
  1781. return (n);
  1782. }
  1783. n+=cnt;
  1784. blkcnt-=cnt;
  1785. blknr+=cnt;
  1786. buffer+=(cnt*ATAPI_READ_BLOCK_SIZE);
  1787. } while (blkcnt > 0);
  1788. return (n);
  1789. }
  1790. /* ------------------------------------------------------------------------- */
  1791. #endif /* CONFIG_ATAPI */
  1792. U_BOOT_CMD(
  1793. ide, 5, 1, do_ide,
  1794. "ide - IDE sub-system\n",
  1795. "reset - reset IDE controller\n"
  1796. "ide info - show available IDE devices\n"
  1797. "ide device [dev] - show or set current device\n"
  1798. "ide part [dev] - print partition table of one or all IDE devices\n"
  1799. "ide read addr blk# cnt\n"
  1800. "ide write addr blk# cnt - read/write `cnt'"
  1801. " blocks starting at block `blk#'\n"
  1802. " to/from memory address `addr'\n"
  1803. );
  1804. U_BOOT_CMD(
  1805. diskboot, 3, 1, do_diskboot,
  1806. "diskboot- boot from IDE device\n",
  1807. "loadAddr dev:part\n"
  1808. );