cmd_ide.c 52 KB

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