cmd_ide.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  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. #ifdef CONFIG_TUNE_PIO
  461. int inline
  462. __ide_set_piomode(int pio_mode)
  463. {
  464. return 0;
  465. }
  466. int inline ide_set_piomode(int pio_mode)
  467. __attribute__((weak, alias("__ide_set_piomode")));
  468. #endif
  469. void ide_init (void)
  470. {
  471. #ifdef CONFIG_IDE_8xx_DIRECT
  472. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  473. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  474. #endif
  475. unsigned char c;
  476. int i, bus;
  477. #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
  478. unsigned int ata_reset_time = ATA_RESET_TIME;
  479. char *s;
  480. #endif
  481. #ifdef CONFIG_AMIGAONEG3SE
  482. unsigned int max_bus_scan;
  483. #endif
  484. #ifdef CONFIG_IDE_8xx_PCCARD
  485. extern int pcmcia_on (void);
  486. extern int ide_devices_found; /* Initialized in check_ide_device() */
  487. #endif /* CONFIG_IDE_8xx_PCCARD */
  488. #ifdef CONFIG_IDE_PREINIT
  489. extern int ide_preinit (void);
  490. WATCHDOG_RESET();
  491. if (ide_preinit ()) {
  492. puts ("ide_preinit failed\n");
  493. return;
  494. }
  495. #endif /* CONFIG_IDE_PREINIT */
  496. #ifdef CONFIG_IDE_8xx_PCCARD
  497. extern int pcmcia_on (void);
  498. extern int ide_devices_found; /* Initialized in check_ide_device() */
  499. WATCHDOG_RESET();
  500. ide_devices_found = 0;
  501. /* initialize the PCMCIA IDE adapter card */
  502. pcmcia_on();
  503. if (!ide_devices_found)
  504. return;
  505. udelay (1000000); /* 1 s */
  506. #endif /* CONFIG_IDE_8xx_PCCARD */
  507. WATCHDOG_RESET();
  508. #ifdef CONFIG_IDE_8xx_DIRECT
  509. /* Initialize PIO timing tables */
  510. for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
  511. pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
  512. gd->bus_clk);
  513. pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
  514. gd->bus_clk);
  515. pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
  516. gd->bus_clk);
  517. debug ( "PIO Mode %d: setup=%2d ns/%d clk"
  518. " len=%3d ns/%d clk"
  519. " hold=%2d ns/%d clk\n",
  520. i,
  521. pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
  522. pio_config_ns[i].t_length, pio_config_clk[i].t_length,
  523. pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
  524. }
  525. #endif /* CONFIG_IDE_8xx_DIRECT */
  526. /* Reset the IDE just to be sure.
  527. * Light LED's to show
  528. */
  529. ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
  530. ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
  531. #ifdef CONFIG_IDE_8xx_DIRECT
  532. /* PCMCIA / IDE initialization for common mem space */
  533. pcmp->pcmc_pgcrb = 0;
  534. /* start in PIO mode 0 - most relaxed timings */
  535. pio_mode = 0;
  536. set_pcmcia_timing (pio_mode);
  537. #endif /* CONFIG_IDE_8xx_DIRECT */
  538. /*
  539. * Wait for IDE to get ready.
  540. * According to spec, this can take up to 31 seconds!
  541. */
  542. #ifndef CONFIG_AMIGAONEG3SE
  543. for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
  544. int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
  545. #else
  546. s = getenv("ide_maxbus");
  547. if (s)
  548. max_bus_scan = simple_strtol(s, NULL, 10);
  549. else
  550. max_bus_scan = CFG_IDE_MAXBUS;
  551. for (bus=0; bus<max_bus_scan; ++bus) {
  552. int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
  553. #endif
  554. #ifdef CONFIG_IDE_8xx_PCCARD
  555. /* Skip non-ide devices from probing */
  556. if ((ide_devices_found & (1 << bus)) == 0) {
  557. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  558. continue;
  559. }
  560. #endif
  561. printf ("Bus %d: ", bus);
  562. ide_bus_ok[bus] = 0;
  563. /* Select device
  564. */
  565. udelay (100000); /* 100 ms */
  566. ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  567. udelay (100000); /* 100 ms */
  568. #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
  569. if ((s = getenv("ide_reset_timeout")) != NULL)
  570. ata_reset_time = simple_strtol(s, NULL, 10);
  571. #endif
  572. i = 0;
  573. do {
  574. udelay (10000); /* 10 ms */
  575. c = ide_inb (dev, ATA_STATUS);
  576. i++;
  577. #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
  578. if (i > (ata_reset_time * 100)) {
  579. #else
  580. if (i > (ATA_RESET_TIME * 100)) {
  581. #endif
  582. puts ("** Timeout **\n");
  583. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  584. #ifdef CONFIG_AMIGAONEG3SE
  585. /* If this is the second bus, the first one was OK */
  586. if (bus != 0) {
  587. ide_bus_ok[bus] = 0;
  588. goto skip_bus;
  589. }
  590. #endif
  591. return;
  592. }
  593. if ((i >= 100) && ((i%100)==0)) {
  594. putc ('.');
  595. }
  596. } while (c & ATA_STAT_BUSY);
  597. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  598. puts ("not available ");
  599. debug ("Status = 0x%02X ", c);
  600. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  601. } else if ((c & ATA_STAT_READY) == 0) {
  602. puts ("not available ");
  603. debug ("Status = 0x%02X ", c);
  604. #endif
  605. } else {
  606. puts ("OK ");
  607. ide_bus_ok[bus] = 1;
  608. }
  609. WATCHDOG_RESET();
  610. }
  611. #ifdef CONFIG_AMIGAONEG3SE
  612. skip_bus:
  613. #endif
  614. putc ('\n');
  615. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  616. curr_device = -1;
  617. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  618. #ifdef CONFIG_IDE_LED
  619. int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
  620. #endif
  621. ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
  622. ide_dev_desc[i].if_type=IF_TYPE_IDE;
  623. ide_dev_desc[i].dev=i;
  624. ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
  625. ide_dev_desc[i].blksz=0;
  626. ide_dev_desc[i].lba=0;
  627. ide_dev_desc[i].block_read=ide_read;
  628. if (!ide_bus_ok[IDE_BUS(i)])
  629. continue;
  630. ide_led (led, 1); /* LED on */
  631. ide_ident(&ide_dev_desc[i]);
  632. ide_led (led, 0); /* LED off */
  633. dev_print(&ide_dev_desc[i]);
  634. /* ide_print (i); */
  635. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  636. init_part (&ide_dev_desc[i]); /* initialize partition type */
  637. if (curr_device < 0)
  638. curr_device = i;
  639. }
  640. }
  641. WATCHDOG_RESET();
  642. }
  643. /* ------------------------------------------------------------------------- */
  644. block_dev_desc_t * ide_get_dev(int dev)
  645. {
  646. return (dev < CFG_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
  647. }
  648. #ifdef CONFIG_IDE_8xx_DIRECT
  649. static void
  650. set_pcmcia_timing (int pmode)
  651. {
  652. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  653. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  654. ulong timings;
  655. debug ("Set timing for PIO Mode %d\n", pmode);
  656. timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
  657. | PCMCIA_SST(pio_config_clk[pmode].t_setup)
  658. | PCMCIA_SL (pio_config_clk[pmode].t_length)
  659. ;
  660. /* IDE 0
  661. */
  662. pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
  663. pcmp->pcmc_por0 = CFG_PCMCIA_POR0
  664. #if (CFG_PCMCIA_POR0 != 0)
  665. | timings
  666. #endif
  667. ;
  668. debug ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
  669. pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
  670. pcmp->pcmc_por1 = CFG_PCMCIA_POR1
  671. #if (CFG_PCMCIA_POR1 != 0)
  672. | timings
  673. #endif
  674. ;
  675. debug ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
  676. pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
  677. pcmp->pcmc_por2 = CFG_PCMCIA_POR2
  678. #if (CFG_PCMCIA_POR2 != 0)
  679. | timings
  680. #endif
  681. ;
  682. debug ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
  683. pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
  684. pcmp->pcmc_por3 = CFG_PCMCIA_POR3
  685. #if (CFG_PCMCIA_POR3 != 0)
  686. | timings
  687. #endif
  688. ;
  689. debug ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
  690. /* IDE 1
  691. */
  692. pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
  693. pcmp->pcmc_por4 = CFG_PCMCIA_POR4
  694. #if (CFG_PCMCIA_POR4 != 0)
  695. | timings
  696. #endif
  697. ;
  698. debug ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
  699. pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
  700. pcmp->pcmc_por5 = CFG_PCMCIA_POR5
  701. #if (CFG_PCMCIA_POR5 != 0)
  702. | timings
  703. #endif
  704. ;
  705. debug ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
  706. pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
  707. pcmp->pcmc_por6 = CFG_PCMCIA_POR6
  708. #if (CFG_PCMCIA_POR6 != 0)
  709. | timings
  710. #endif
  711. ;
  712. debug ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
  713. pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
  714. pcmp->pcmc_por7 = CFG_PCMCIA_POR7
  715. #if (CFG_PCMCIA_POR7 != 0)
  716. | timings
  717. #endif
  718. ;
  719. debug ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
  720. }
  721. #endif /* CONFIG_IDE_8xx_DIRECT */
  722. /* ------------------------------------------------------------------------- */
  723. #ifdef __PPC__
  724. # ifdef CONFIG_AMIGAONEG3SE
  725. static void
  726. output_data_short(int dev, ulong *sect_buf, int words)
  727. {
  728. ushort *dbuf;
  729. volatile ushort *pbuf;
  730. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  731. dbuf = (ushort *)sect_buf;
  732. while (words--) {
  733. EIEIO;
  734. *pbuf = *dbuf++;
  735. EIEIO;
  736. }
  737. if (words&1)
  738. *pbuf = 0;
  739. }
  740. # endif /* CONFIG_AMIGAONEG3SE */
  741. #endif /* __PPC_ */
  742. /* We only need to swap data if we are running on a big endian cpu. */
  743. /* But Au1x00 cpu:s already swaps data in big endian mode! */
  744. #if defined(__LITTLE_ENDIAN) || ( defined(CONFIG_AU1X00) && !defined(CONFIG_GTH2) )
  745. #define input_swap_data(x,y,z) input_data(x,y,z)
  746. #else
  747. static void
  748. input_swap_data(int dev, ulong *sect_buf, int words)
  749. {
  750. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  751. uchar i;
  752. volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  753. volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  754. ushort *dbuf = (ushort *)sect_buf;
  755. while (words--) {
  756. for (i=0; i<2; i++) {
  757. *(((uchar *)(dbuf)) + 1) = *pbuf_even;
  758. *(uchar *)dbuf = *pbuf_odd;
  759. dbuf+=1;
  760. }
  761. }
  762. #else
  763. volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  764. ushort *dbuf = (ushort *)sect_buf;
  765. debug("in input swap data base for read is %lx\n", (unsigned long) pbuf);
  766. while (words--) {
  767. #ifdef __MIPS__
  768. *dbuf++ = swab16p((u16*)pbuf);
  769. *dbuf++ = swab16p((u16*)pbuf);
  770. #elif defined(CONFIG_PCS440EP)
  771. *dbuf++ = *pbuf;
  772. *dbuf++ = *pbuf;
  773. #else
  774. *dbuf++ = ld_le16(pbuf);
  775. *dbuf++ = ld_le16(pbuf);
  776. #endif /* !MIPS */
  777. }
  778. #endif
  779. }
  780. #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
  781. #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA) || defined(CONFIG_SH)
  782. static void
  783. output_data(int dev, ulong *sect_buf, int words)
  784. {
  785. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  786. uchar *dbuf;
  787. volatile uchar *pbuf_even;
  788. volatile uchar *pbuf_odd;
  789. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  790. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  791. dbuf = (uchar *)sect_buf;
  792. while (words--) {
  793. EIEIO;
  794. *pbuf_even = *dbuf++;
  795. EIEIO;
  796. *pbuf_odd = *dbuf++;
  797. EIEIO;
  798. *pbuf_even = *dbuf++;
  799. EIEIO;
  800. *pbuf_odd = *dbuf++;
  801. }
  802. #else
  803. ushort *dbuf;
  804. volatile ushort *pbuf;
  805. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  806. dbuf = (ushort *)sect_buf;
  807. while (words--) {
  808. #if defined(CONFIG_PCS440EP)
  809. /* not tested, because CF was write protected */
  810. EIEIO;
  811. *pbuf = ld_le16(dbuf++);
  812. EIEIO;
  813. *pbuf = ld_le16(dbuf++);
  814. #else
  815. EIEIO;
  816. *pbuf = *dbuf++;
  817. EIEIO;
  818. *pbuf = *dbuf++;
  819. #endif
  820. }
  821. #endif
  822. }
  823. #else /* ! __PPC__ */
  824. static void
  825. output_data(int dev, ulong *sect_buf, int words)
  826. {
  827. outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
  828. }
  829. #endif /* __PPC__ */
  830. #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA) || defined(CONFIG_SH)
  831. static void
  832. input_data(int dev, ulong *sect_buf, int words)
  833. {
  834. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  835. uchar *dbuf;
  836. volatile uchar *pbuf_even;
  837. volatile uchar *pbuf_odd;
  838. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  839. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  840. dbuf = (uchar *)sect_buf;
  841. while (words--) {
  842. *dbuf++ = *pbuf_even;
  843. EIEIO;
  844. SYNC;
  845. *dbuf++ = *pbuf_odd;
  846. EIEIO;
  847. SYNC;
  848. *dbuf++ = *pbuf_even;
  849. EIEIO;
  850. SYNC;
  851. *dbuf++ = *pbuf_odd;
  852. EIEIO;
  853. SYNC;
  854. }
  855. #else
  856. ushort *dbuf;
  857. volatile ushort *pbuf;
  858. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  859. dbuf = (ushort *)sect_buf;
  860. debug("in input data base for read is %lx\n", (unsigned long) pbuf);
  861. while (words--) {
  862. #if defined(CONFIG_PCS440EP)
  863. EIEIO;
  864. *dbuf++ = ld_le16(pbuf);
  865. EIEIO;
  866. *dbuf++ = ld_le16(pbuf);
  867. #else
  868. EIEIO;
  869. *dbuf++ = *pbuf;
  870. EIEIO;
  871. *dbuf++ = *pbuf;
  872. #endif
  873. }
  874. #endif
  875. }
  876. #else /* ! __PPC__ */
  877. static void
  878. input_data(int dev, ulong *sect_buf, int words)
  879. {
  880. insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
  881. }
  882. #endif /* __PPC__ */
  883. #ifdef CONFIG_AMIGAONEG3SE
  884. static void
  885. input_data_short(int dev, ulong *sect_buf, int words)
  886. {
  887. ushort *dbuf;
  888. volatile ushort *pbuf;
  889. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  890. dbuf = (ushort *)sect_buf;
  891. while (words--) {
  892. EIEIO;
  893. *dbuf++ = *pbuf;
  894. EIEIO;
  895. }
  896. if (words&1) {
  897. ushort dummy;
  898. dummy = *pbuf;
  899. }
  900. }
  901. #endif
  902. /* -------------------------------------------------------------------------
  903. */
  904. static void ide_ident (block_dev_desc_t *dev_desc)
  905. {
  906. ulong iobuf[ATA_SECTORWORDS];
  907. unsigned char c;
  908. hd_driveid_t *iop = (hd_driveid_t *)iobuf;
  909. #ifdef CONFIG_AMIGAONEG3SE
  910. int max_bus_scan;
  911. char *s;
  912. #endif
  913. #ifdef CONFIG_ATAPI
  914. int retries = 0;
  915. int do_retry = 0;
  916. #endif
  917. #ifdef CONFIG_TUNE_PIO
  918. int pio_mode;
  919. #endif
  920. #if 0
  921. int mode, cycle_time;
  922. #endif
  923. int device;
  924. device=dev_desc->dev;
  925. printf (" Device %d: ", device);
  926. #ifdef CONFIG_AMIGAONEG3SE
  927. s = getenv("ide_maxbus");
  928. if (s) {
  929. max_bus_scan = simple_strtol(s, NULL, 10);
  930. } else {
  931. max_bus_scan = CFG_IDE_MAXBUS;
  932. }
  933. if (device >= max_bus_scan*2) {
  934. dev_desc->type=DEV_TYPE_UNKNOWN;
  935. return;
  936. }
  937. #endif
  938. ide_led (DEVICE_LED(device), 1); /* LED on */
  939. /* Select device
  940. */
  941. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  942. dev_desc->if_type=IF_TYPE_IDE;
  943. #ifdef CONFIG_ATAPI
  944. do_retry = 0;
  945. retries = 0;
  946. /* Warning: This will be tricky to read */
  947. while (retries <= 1) {
  948. /* check signature */
  949. if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
  950. (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
  951. (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
  952. (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
  953. /* ATAPI Signature found */
  954. dev_desc->if_type=IF_TYPE_ATAPI;
  955. /* Start Ident Command
  956. */
  957. ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
  958. /*
  959. * Wait for completion - ATAPI devices need more time
  960. * to become ready
  961. */
  962. c = ide_wait (device, ATAPI_TIME_OUT);
  963. } else
  964. #endif
  965. {
  966. /* Start Ident Command
  967. */
  968. ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
  969. /* Wait for completion
  970. */
  971. c = ide_wait (device, IDE_TIME_OUT);
  972. }
  973. ide_led (DEVICE_LED(device), 0); /* LED off */
  974. if (((c & ATA_STAT_DRQ) == 0) ||
  975. ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
  976. #ifdef CONFIG_ATAPI
  977. #ifdef CONFIG_AMIGAONEG3SE
  978. s = getenv("ide_doreset");
  979. if (s && strcmp(s, "on") == 0)
  980. #endif
  981. {
  982. /* Need to soft reset the device in case it's an ATAPI... */
  983. debug ("Retrying...\n");
  984. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  985. udelay(100000);
  986. ide_outb (device, ATA_COMMAND, 0x08);
  987. udelay (500000); /* 500 ms */
  988. }
  989. /* Select device
  990. */
  991. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  992. retries++;
  993. #else
  994. return;
  995. #endif
  996. }
  997. #ifdef CONFIG_ATAPI
  998. else
  999. break;
  1000. } /* see above - ugly to read */
  1001. if (retries == 2) /* Not found */
  1002. return;
  1003. #endif
  1004. input_swap_data (device, iobuf, ATA_SECTORWORDS);
  1005. ident_cpy ((unsigned char*)dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
  1006. ident_cpy ((unsigned char*)dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
  1007. ident_cpy ((unsigned char*)dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
  1008. #ifdef __LITTLE_ENDIAN
  1009. /*
  1010. * firmware revision and model number have Big Endian Byte
  1011. * order in Word. Convert both to little endian.
  1012. *
  1013. * See CF+ and CompactFlash Specification Revision 2.0:
  1014. * 6.2.1.6: Identfy Drive, Table 39 for more details
  1015. */
  1016. strswab (dev_desc->revision);
  1017. strswab (dev_desc->vendor);
  1018. #endif /* __LITTLE_ENDIAN */
  1019. if ((iop->config & 0x0080)==0x0080)
  1020. dev_desc->removable = 1;
  1021. else
  1022. dev_desc->removable = 0;
  1023. #ifdef CONFIG_TUNE_PIO
  1024. /* Mode 0 - 2 only, are directly determined by word 51. */
  1025. pio_mode = iop->tPIO;
  1026. if (pio_mode > 2) {
  1027. printf("WARNING: Invalid PIO (word 51 = %d).\n", pio_mode);
  1028. pio_mode = 0; /* Force it to dead slow, and hope for the best... */
  1029. }
  1030. /* Any CompactFlash Storage Card that supports PIO mode 3 or above
  1031. * shall set bit 1 of word 53 to one and support the fields contained
  1032. * in words 64 through 70.
  1033. */
  1034. if (iop->field_valid & 0x02) {
  1035. /* Mode 3 and above are possible. Check in order from slow
  1036. * to fast, so we wind up with the highest mode allowed.
  1037. */
  1038. if (iop->eide_pio_modes & 0x01)
  1039. pio_mode = 3;
  1040. if (iop->eide_pio_modes & 0x02)
  1041. pio_mode = 4;
  1042. if (ata_id_is_cfa((u16 *)iop)) {
  1043. if ((iop->cf_advanced_caps & 0x07) == 0x01)
  1044. pio_mode = 5;
  1045. if ((iop->cf_advanced_caps & 0x07) == 0x02)
  1046. pio_mode = 6;
  1047. }
  1048. }
  1049. /* System-specific, depends on bus speeds, etc. */
  1050. ide_set_piomode(pio_mode);
  1051. #endif /* CONFIG_TUNE_PIO */
  1052. #if 0
  1053. /*
  1054. * Drive PIO mode autoselection
  1055. */
  1056. mode = iop->tPIO;
  1057. printf ("tPIO = 0x%02x = %d\n",mode, mode);
  1058. if (mode > 2) { /* 2 is maximum allowed tPIO value */
  1059. mode = 2;
  1060. debug ("Override tPIO -> 2\n");
  1061. }
  1062. if (iop->field_valid & 2) { /* drive implements ATA2? */
  1063. debug ("Drive implements ATA2\n");
  1064. if (iop->capability & 8) { /* drive supports use_iordy? */
  1065. cycle_time = iop->eide_pio_iordy;
  1066. } else {
  1067. cycle_time = iop->eide_pio;
  1068. }
  1069. debug ("cycle time = %d\n", cycle_time);
  1070. mode = 4;
  1071. if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
  1072. if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
  1073. if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
  1074. if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
  1075. }
  1076. printf ("PIO mode to use: PIO %d\n", mode);
  1077. #endif /* 0 */
  1078. #ifdef CONFIG_ATAPI
  1079. if (dev_desc->if_type==IF_TYPE_ATAPI) {
  1080. atapi_inquiry(dev_desc);
  1081. return;
  1082. }
  1083. #endif /* CONFIG_ATAPI */
  1084. #ifdef __BIG_ENDIAN
  1085. /* swap shorts */
  1086. dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
  1087. #else /* ! __BIG_ENDIAN */
  1088. /*
  1089. * do not swap shorts on little endian
  1090. *
  1091. * See CF+ and CompactFlash Specification Revision 2.0:
  1092. * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
  1093. */
  1094. dev_desc->lba = iop->lba_capacity;
  1095. #endif /* __BIG_ENDIAN */
  1096. #ifdef CONFIG_LBA48
  1097. if (iop->command_set_2 & 0x0400) { /* LBA 48 support */
  1098. dev_desc->lba48 = 1;
  1099. dev_desc->lba = (unsigned long long)iop->lba48_capacity[0] |
  1100. ((unsigned long long)iop->lba48_capacity[1] << 16) |
  1101. ((unsigned long long)iop->lba48_capacity[2] << 32) |
  1102. ((unsigned long long)iop->lba48_capacity[3] << 48);
  1103. } else {
  1104. dev_desc->lba48 = 0;
  1105. }
  1106. #endif /* CONFIG_LBA48 */
  1107. /* assuming HD */
  1108. dev_desc->type=DEV_TYPE_HARDDISK;
  1109. dev_desc->blksz=ATA_BLOCKSIZE;
  1110. dev_desc->lun=0; /* just to fill something in... */
  1111. #if 0 /* only used to test the powersaving mode,
  1112. * if enabled, the drive goes after 5 sec
  1113. * in standby mode */
  1114. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1115. c = ide_wait (device, IDE_TIME_OUT);
  1116. ide_outb (device, ATA_SECT_CNT, 1);
  1117. ide_outb (device, ATA_LBA_LOW, 0);
  1118. ide_outb (device, ATA_LBA_MID, 0);
  1119. ide_outb (device, ATA_LBA_HIGH, 0);
  1120. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1121. ide_outb (device, ATA_COMMAND, 0xe3);
  1122. udelay (50);
  1123. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1124. #endif
  1125. }
  1126. /* ------------------------------------------------------------------------- */
  1127. ulong ide_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  1128. {
  1129. ulong n = 0;
  1130. unsigned char c;
  1131. unsigned char pwrsave=0; /* power save */
  1132. #ifdef CONFIG_LBA48
  1133. unsigned char lba48 = 0;
  1134. if (blknr & 0x0000fffff0000000ULL) {
  1135. /* more than 28 bits used, use 48bit mode */
  1136. lba48 = 1;
  1137. }
  1138. #endif
  1139. debug ("ide_read dev %d start %qX, blocks %lX buffer at %lX\n",
  1140. device, blknr, blkcnt, (ulong)buffer);
  1141. ide_led (DEVICE_LED(device), 1); /* LED on */
  1142. /* Select device
  1143. */
  1144. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1145. c = ide_wait (device, IDE_TIME_OUT);
  1146. if (c & ATA_STAT_BUSY) {
  1147. printf ("IDE read: device %d not ready\n", device);
  1148. goto IDE_READ_E;
  1149. }
  1150. /* first check if the drive is in Powersaving mode, if yes,
  1151. * increase the timeout value */
  1152. ide_outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
  1153. udelay (50);
  1154. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1155. if (c & ATA_STAT_BUSY) {
  1156. printf ("IDE read: device %d not ready\n", device);
  1157. goto IDE_READ_E;
  1158. }
  1159. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1160. printf ("No Powersaving mode %X\n", c);
  1161. } else {
  1162. c = ide_inb(device,ATA_SECT_CNT);
  1163. debug ("Powersaving %02X\n",c);
  1164. if(c==0)
  1165. pwrsave=1;
  1166. }
  1167. while (blkcnt-- > 0) {
  1168. c = ide_wait (device, IDE_TIME_OUT);
  1169. if (c & ATA_STAT_BUSY) {
  1170. printf ("IDE read: device %d not ready\n", device);
  1171. break;
  1172. }
  1173. #ifdef CONFIG_LBA48
  1174. if (lba48) {
  1175. /* write high bits */
  1176. ide_outb (device, ATA_SECT_CNT, 0);
  1177. ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  1178. #ifdef CFG_64BIT_LBA
  1179. ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  1180. ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  1181. #else
  1182. ide_outb (device, ATA_LBA_MID, 0);
  1183. ide_outb (device, ATA_LBA_HIGH, 0);
  1184. #endif
  1185. }
  1186. #endif
  1187. ide_outb (device, ATA_SECT_CNT, 1);
  1188. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  1189. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  1190. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  1191. #ifdef CONFIG_LBA48
  1192. if (lba48) {
  1193. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
  1194. ide_outb (device, ATA_COMMAND, ATA_CMD_READ_EXT);
  1195. } else
  1196. #endif
  1197. {
  1198. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  1199. ATA_DEVICE(device) |
  1200. ((blknr >> 24) & 0xF) );
  1201. ide_outb (device, ATA_COMMAND, ATA_CMD_READ);
  1202. }
  1203. udelay (50);
  1204. if(pwrsave) {
  1205. c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
  1206. pwrsave=0;
  1207. } else {
  1208. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1209. }
  1210. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  1211. #if defined(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
  1212. printf ("Error (no IRQ) dev %d blk %qd: status 0x%02x\n",
  1213. device, blknr, c);
  1214. #else
  1215. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  1216. device, (ulong)blknr, c);
  1217. #endif
  1218. break;
  1219. }
  1220. input_data (device, buffer, ATA_SECTORWORDS);
  1221. (void) ide_inb (device, ATA_STATUS); /* clear IRQ */
  1222. ++n;
  1223. ++blknr;
  1224. buffer += ATA_BLOCKSIZE;
  1225. }
  1226. IDE_READ_E:
  1227. ide_led (DEVICE_LED(device), 0); /* LED off */
  1228. return (n);
  1229. }
  1230. /* ------------------------------------------------------------------------- */
  1231. ulong ide_write (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  1232. {
  1233. ulong n = 0;
  1234. unsigned char c;
  1235. #ifdef CONFIG_LBA48
  1236. unsigned char lba48 = 0;
  1237. if (blknr & 0x0000fffff0000000ULL) {
  1238. /* more than 28 bits used, use 48bit mode */
  1239. lba48 = 1;
  1240. }
  1241. #endif
  1242. ide_led (DEVICE_LED(device), 1); /* LED on */
  1243. /* Select device
  1244. */
  1245. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1246. while (blkcnt-- > 0) {
  1247. c = ide_wait (device, IDE_TIME_OUT);
  1248. if (c & ATA_STAT_BUSY) {
  1249. printf ("IDE read: device %d not ready\n", device);
  1250. goto WR_OUT;
  1251. }
  1252. #ifdef CONFIG_LBA48
  1253. if (lba48) {
  1254. /* write high bits */
  1255. ide_outb (device, ATA_SECT_CNT, 0);
  1256. ide_outb (device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  1257. #ifdef CFG_64BIT_LBA
  1258. ide_outb (device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  1259. ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  1260. #else
  1261. ide_outb (device, ATA_LBA_MID, 0);
  1262. ide_outb (device, ATA_LBA_HIGH, 0);
  1263. #endif
  1264. }
  1265. #endif
  1266. ide_outb (device, ATA_SECT_CNT, 1);
  1267. ide_outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  1268. ide_outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  1269. ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  1270. #ifdef CONFIG_LBA48
  1271. if (lba48) {
  1272. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device) );
  1273. ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
  1274. } else
  1275. #endif
  1276. {
  1277. ide_outb (device, ATA_DEV_HD, ATA_LBA |
  1278. ATA_DEVICE(device) |
  1279. ((blknr >> 24) & 0xF) );
  1280. ide_outb (device, ATA_COMMAND, ATA_CMD_WRITE);
  1281. }
  1282. udelay (50);
  1283. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  1284. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  1285. #if defined(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
  1286. printf ("Error (no IRQ) dev %d blk %qd: status 0x%02x\n",
  1287. device, blknr, c);
  1288. #else
  1289. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  1290. device, (ulong)blknr, c);
  1291. #endif
  1292. goto WR_OUT;
  1293. }
  1294. output_data (device, buffer, ATA_SECTORWORDS);
  1295. c = ide_inb (device, ATA_STATUS); /* clear IRQ */
  1296. ++n;
  1297. ++blknr;
  1298. buffer += ATA_BLOCKSIZE;
  1299. }
  1300. WR_OUT:
  1301. ide_led (DEVICE_LED(device), 0); /* LED off */
  1302. return (n);
  1303. }
  1304. /* ------------------------------------------------------------------------- */
  1305. /*
  1306. * copy src to dest, skipping leading and trailing blanks and null
  1307. * terminate the string
  1308. * "len" is the size of available memory including the terminating '\0'
  1309. */
  1310. static void ident_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
  1311. {
  1312. unsigned char *end, *last;
  1313. last = dst;
  1314. end = src + len - 1;
  1315. /* reserve space for '\0' */
  1316. if (len < 2)
  1317. goto OUT;
  1318. /* skip leading white space */
  1319. while ((*src) && (src<end) && (*src==' '))
  1320. ++src;
  1321. /* copy string, omitting trailing white space */
  1322. while ((*src) && (src<end)) {
  1323. *dst++ = *src;
  1324. if (*src++ != ' ')
  1325. last = dst;
  1326. }
  1327. OUT:
  1328. *last = '\0';
  1329. }
  1330. /* ------------------------------------------------------------------------- */
  1331. /*
  1332. * Wait until Busy bit is off, or timeout (in ms)
  1333. * Return last status
  1334. */
  1335. static uchar ide_wait (int dev, ulong t)
  1336. {
  1337. ulong delay = 10 * t; /* poll every 100 us */
  1338. uchar c;
  1339. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  1340. udelay (100);
  1341. if (delay-- == 0) {
  1342. break;
  1343. }
  1344. }
  1345. return (c);
  1346. }
  1347. /* ------------------------------------------------------------------------- */
  1348. #ifdef CONFIG_IDE_RESET
  1349. extern void ide_set_reset(int idereset);
  1350. static void ide_reset (void)
  1351. {
  1352. #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
  1353. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  1354. #endif
  1355. int i;
  1356. curr_device = -1;
  1357. for (i=0; i<CFG_IDE_MAXBUS; ++i)
  1358. ide_bus_ok[i] = 0;
  1359. for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
  1360. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  1361. ide_set_reset (1); /* assert reset */
  1362. /* the reset signal shall be asserted for et least 25 us */
  1363. udelay(25);
  1364. WATCHDOG_RESET();
  1365. #ifdef CFG_PB_12V_ENABLE
  1366. immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
  1367. immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
  1368. immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
  1369. immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
  1370. /* wait 500 ms for the voltage to stabilize
  1371. */
  1372. for (i=0; i<500; ++i) {
  1373. udelay (1000);
  1374. }
  1375. immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
  1376. #endif /* CFG_PB_12V_ENABLE */
  1377. #ifdef CFG_PB_IDE_MOTOR
  1378. /* configure IDE Motor voltage monitor pin as input */
  1379. immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
  1380. immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
  1381. immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
  1382. /* wait up to 1 s for the motor voltage to stabilize
  1383. */
  1384. for (i=0; i<1000; ++i) {
  1385. if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
  1386. break;
  1387. }
  1388. udelay (1000);
  1389. }
  1390. if (i == 1000) { /* Timeout */
  1391. printf ("\nWarning: 5V for IDE Motor missing\n");
  1392. # ifdef CONFIG_STATUS_LED
  1393. # ifdef STATUS_LED_YELLOW
  1394. status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
  1395. # endif
  1396. # ifdef STATUS_LED_GREEN
  1397. status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
  1398. # endif
  1399. # endif /* CONFIG_STATUS_LED */
  1400. }
  1401. #endif /* CFG_PB_IDE_MOTOR */
  1402. WATCHDOG_RESET();
  1403. /* de-assert RESET signal */
  1404. ide_set_reset(0);
  1405. /* wait 250 ms */
  1406. for (i=0; i<250; ++i) {
  1407. udelay (1000);
  1408. }
  1409. }
  1410. #endif /* CONFIG_IDE_RESET */
  1411. /* ------------------------------------------------------------------------- */
  1412. #if defined(CONFIG_IDE_LED) && \
  1413. !defined(CONFIG_AMIGAONEG3SE)&& \
  1414. !defined(CONFIG_CPC45) && \
  1415. !defined(CONFIG_HMI10) && \
  1416. !defined(CONFIG_KUP4K) && \
  1417. !defined(CONFIG_KUP4X)
  1418. static uchar led_buffer = 0; /* Buffer for current LED status */
  1419. static void ide_led (uchar led, uchar status)
  1420. {
  1421. uchar *led_port = LED_PORT;
  1422. if (status) { /* switch LED on */
  1423. led_buffer |= led;
  1424. } else { /* switch LED off */
  1425. led_buffer &= ~led;
  1426. }
  1427. *led_port = led_buffer;
  1428. }
  1429. #endif /* CONFIG_IDE_LED */
  1430. /* ------------------------------------------------------------------------- */
  1431. #ifdef CONFIG_ATAPI
  1432. /****************************************************************************
  1433. * ATAPI Support
  1434. */
  1435. #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA)
  1436. /* since ATAPI may use commands with not 4 bytes alligned length
  1437. * we have our own transfer functions, 2 bytes alligned */
  1438. static void
  1439. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1440. {
  1441. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  1442. uchar *dbuf;
  1443. volatile uchar *pbuf_even;
  1444. volatile uchar *pbuf_odd;
  1445. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  1446. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  1447. while (shorts--) {
  1448. EIEIO;
  1449. *pbuf_even = *dbuf++;
  1450. EIEIO;
  1451. *pbuf_odd = *dbuf++;
  1452. }
  1453. #else
  1454. ushort *dbuf;
  1455. volatile ushort *pbuf;
  1456. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1457. dbuf = (ushort *)sect_buf;
  1458. debug ("in output data shorts base for read is %lx\n", (unsigned long) pbuf);
  1459. while (shorts--) {
  1460. EIEIO;
  1461. *pbuf = *dbuf++;
  1462. }
  1463. #endif
  1464. }
  1465. static void
  1466. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1467. {
  1468. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  1469. uchar *dbuf;
  1470. volatile uchar *pbuf_even;
  1471. volatile uchar *pbuf_odd;
  1472. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  1473. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  1474. while (shorts--) {
  1475. EIEIO;
  1476. *dbuf++ = *pbuf_even;
  1477. EIEIO;
  1478. *dbuf++ = *pbuf_odd;
  1479. }
  1480. #else
  1481. ushort *dbuf;
  1482. volatile ushort *pbuf;
  1483. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1484. dbuf = (ushort *)sect_buf;
  1485. debug("in input data shorts base for read is %lx\n", (unsigned long) pbuf);
  1486. while (shorts--) {
  1487. EIEIO;
  1488. *dbuf++ = *pbuf;
  1489. }
  1490. #endif
  1491. }
  1492. #else /* ! __PPC__ */
  1493. static void
  1494. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1495. {
  1496. outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
  1497. }
  1498. static void
  1499. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1500. {
  1501. insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
  1502. }
  1503. #endif /* __PPC__ */
  1504. /*
  1505. * Wait until (Status & mask) == res, or timeout (in ms)
  1506. * Return last status
  1507. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  1508. * and then they set their DRQ Bit
  1509. */
  1510. static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
  1511. {
  1512. ulong delay = 10 * t; /* poll every 100 us */
  1513. uchar c;
  1514. c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
  1515. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  1516. /* break if error occurs (doesn't make sense to wait more) */
  1517. if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
  1518. break;
  1519. udelay (100);
  1520. if (delay-- == 0) {
  1521. break;
  1522. }
  1523. }
  1524. return (c);
  1525. }
  1526. /*
  1527. * issue an atapi command
  1528. */
  1529. unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
  1530. {
  1531. unsigned char c,err,mask,res;
  1532. int n;
  1533. ide_led (DEVICE_LED(device), 1); /* LED on */
  1534. /* Select device
  1535. */
  1536. mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
  1537. res = 0;
  1538. #ifdef CONFIG_AMIGAONEG3SE
  1539. # warning THF: Removed LBA mode ???
  1540. #endif
  1541. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1542. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1543. if ((c & mask) != res) {
  1544. printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
  1545. err=0xFF;
  1546. goto AI_OUT;
  1547. }
  1548. /* write taskfile */
  1549. ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  1550. ide_outb (device, ATA_SECT_CNT, 0);
  1551. ide_outb (device, ATA_SECT_NUM, 0);
  1552. ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
  1553. ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
  1554. #ifdef CONFIG_AMIGAONEG3SE
  1555. # warning THF: Removed LBA mode ???
  1556. #endif
  1557. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1558. ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
  1559. udelay (50);
  1560. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1561. res = ATA_STAT_DRQ;
  1562. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1563. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  1564. printf ("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
  1565. err=0xFF;
  1566. goto AI_OUT;
  1567. }
  1568. output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
  1569. /* ATAPI Command written wait for completition */
  1570. udelay (5000); /* device must set bsy */
  1571. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1572. /* if no data wait for DRQ = 0 BSY = 0
  1573. * if data wait for DRQ = 1 BSY = 0 */
  1574. res=0;
  1575. if(buflen)
  1576. res = ATA_STAT_DRQ;
  1577. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1578. if ((c & mask) != res ) {
  1579. if (c & ATA_STAT_ERR) {
  1580. err=(ide_inb(device,ATA_ERROR_REG))>>4;
  1581. debug ("atapi_issue 1 returned sense key %X status %02X\n",err,c);
  1582. } else {
  1583. printf ("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
  1584. err=0xFF;
  1585. }
  1586. goto AI_OUT;
  1587. }
  1588. n=ide_inb(device, ATA_CYL_HIGH);
  1589. n<<=8;
  1590. n+=ide_inb(device, ATA_CYL_LOW);
  1591. if(n>buflen) {
  1592. printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
  1593. err=0xff;
  1594. goto AI_OUT;
  1595. }
  1596. if((n==0)&&(buflen<0)) {
  1597. printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
  1598. err=0xff;
  1599. goto AI_OUT;
  1600. }
  1601. if(n!=buflen) {
  1602. debug ("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
  1603. }
  1604. if(n!=0) { /* data transfer */
  1605. debug ("ATAPI_ISSUE: %d Bytes to transfer\n",n);
  1606. /* we transfer shorts */
  1607. n>>=1;
  1608. /* ok now decide if it is an in or output */
  1609. if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
  1610. debug ("Write to device\n");
  1611. output_data_shorts(device,(unsigned short *)buffer,n);
  1612. } else {
  1613. debug ("Read from device @ %p shorts %d\n",buffer,n);
  1614. input_data_shorts(device,(unsigned short *)buffer,n);
  1615. }
  1616. }
  1617. udelay(5000); /* seems that some CD ROMs need this... */
  1618. mask = ATA_STAT_BUSY|ATA_STAT_ERR;
  1619. res=0;
  1620. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1621. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1622. err=(ide_inb(device,ATA_ERROR_REG) >> 4);
  1623. debug ("atapi_issue 2 returned sense key %X status %X\n",err,c);
  1624. } else {
  1625. err = 0;
  1626. }
  1627. AI_OUT:
  1628. ide_led (DEVICE_LED(device), 0); /* LED off */
  1629. return (err);
  1630. }
  1631. /*
  1632. * sending the command to atapi_issue. If an status other than good
  1633. * returns, an request_sense will be issued
  1634. */
  1635. #define ATAPI_DRIVE_NOT_READY 100
  1636. #define ATAPI_UNIT_ATTN 10
  1637. unsigned char atapi_issue_autoreq (int device,
  1638. unsigned char* ccb,
  1639. int ccblen,
  1640. unsigned char *buffer,
  1641. int buflen)
  1642. {
  1643. unsigned char sense_data[18],sense_ccb[12];
  1644. unsigned char res,key,asc,ascq;
  1645. int notready,unitattn;
  1646. #ifdef CONFIG_AMIGAONEG3SE
  1647. char *s;
  1648. unsigned int timeout, retrycnt;
  1649. s = getenv("ide_cd_timeout");
  1650. timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
  1651. retrycnt = 0;
  1652. #endif
  1653. unitattn=ATAPI_UNIT_ATTN;
  1654. notready=ATAPI_DRIVE_NOT_READY;
  1655. retry:
  1656. res= atapi_issue(device,ccb,ccblen,buffer,buflen);
  1657. if (res==0)
  1658. return (0); /* Ok */
  1659. if (res==0xFF)
  1660. return (0xFF); /* error */
  1661. debug ("(auto_req)atapi_issue returned sense key %X\n",res);
  1662. memset(sense_ccb,0,sizeof(sense_ccb));
  1663. memset(sense_data,0,sizeof(sense_data));
  1664. sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
  1665. sense_ccb[4]=18; /* allocation Length */
  1666. res=atapi_issue(device,sense_ccb,12,sense_data,18);
  1667. key=(sense_data[2]&0xF);
  1668. asc=(sense_data[12]);
  1669. ascq=(sense_data[13]);
  1670. debug ("ATAPI_CMD_REQ_SENSE returned %x\n",res);
  1671. debug (" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1672. sense_data[0],
  1673. key,
  1674. asc,
  1675. ascq);
  1676. if((key==0))
  1677. return 0; /* ok device ready */
  1678. if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
  1679. if(unitattn-->0) {
  1680. udelay(200*1000);
  1681. goto retry;
  1682. }
  1683. printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
  1684. goto error;
  1685. }
  1686. if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
  1687. if (notready-->0) {
  1688. udelay(200*1000);
  1689. goto retry;
  1690. }
  1691. printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
  1692. goto error;
  1693. }
  1694. if(asc==0x3a) {
  1695. debug ("Media not present\n");
  1696. goto error;
  1697. }
  1698. #ifdef CONFIG_AMIGAONEG3SE
  1699. if ((sense_data[2]&0xF)==0x0B) {
  1700. debug ("ABORTED COMMAND...retry\n");
  1701. if (retrycnt++ < 4)
  1702. goto retry;
  1703. return (0xFF);
  1704. }
  1705. if ((sense_data[2]&0xf) == 0x02 &&
  1706. sense_data[12] == 0x04 &&
  1707. sense_data[13] == 0x01 ) {
  1708. debug ("Waiting for unit to become active\n");
  1709. udelay(timeout);
  1710. if (retrycnt++ < 4)
  1711. goto retry;
  1712. return 0xFF;
  1713. }
  1714. #endif /* CONFIG_AMIGAONEG3SE */
  1715. printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1716. error:
  1717. debug ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1718. return (0xFF);
  1719. }
  1720. static void atapi_inquiry(block_dev_desc_t * dev_desc)
  1721. {
  1722. unsigned char ccb[12]; /* Command descriptor block */
  1723. unsigned char iobuf[64]; /* temp buf */
  1724. unsigned char c;
  1725. int device;
  1726. device=dev_desc->dev;
  1727. dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
  1728. dev_desc->block_read=atapi_read;
  1729. memset(ccb,0,sizeof(ccb));
  1730. memset(iobuf,0,sizeof(iobuf));
  1731. ccb[0]=ATAPI_CMD_INQUIRY;
  1732. ccb[4]=40; /* allocation Legnth */
  1733. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
  1734. debug ("ATAPI_CMD_INQUIRY returned %x\n",c);
  1735. if (c!=0)
  1736. return;
  1737. /* copy device ident strings */
  1738. ident_cpy((unsigned char*)dev_desc->vendor,&iobuf[8],8);
  1739. ident_cpy((unsigned char*)dev_desc->product,&iobuf[16],16);
  1740. ident_cpy((unsigned char*)dev_desc->revision,&iobuf[32],5);
  1741. dev_desc->lun=0;
  1742. dev_desc->lba=0;
  1743. dev_desc->blksz=0;
  1744. dev_desc->type=iobuf[0] & 0x1f;
  1745. if ((iobuf[1]&0x80)==0x80)
  1746. dev_desc->removable = 1;
  1747. else
  1748. dev_desc->removable = 0;
  1749. memset(ccb,0,sizeof(ccb));
  1750. memset(iobuf,0,sizeof(iobuf));
  1751. ccb[0]=ATAPI_CMD_START_STOP;
  1752. ccb[4]=0x03; /* start */
  1753. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1754. debug ("ATAPI_CMD_START_STOP returned %x\n",c);
  1755. if (c!=0)
  1756. return;
  1757. memset(ccb,0,sizeof(ccb));
  1758. memset(iobuf,0,sizeof(iobuf));
  1759. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1760. debug ("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
  1761. if (c!=0)
  1762. return;
  1763. memset(ccb,0,sizeof(ccb));
  1764. memset(iobuf,0,sizeof(iobuf));
  1765. ccb[0]=ATAPI_CMD_READ_CAP;
  1766. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
  1767. debug ("ATAPI_CMD_READ_CAP returned %x\n",c);
  1768. if (c!=0)
  1769. return;
  1770. debug ("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1771. iobuf[0],iobuf[1],iobuf[2],iobuf[3],
  1772. iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
  1773. dev_desc->lba =((unsigned long)iobuf[0]<<24) +
  1774. ((unsigned long)iobuf[1]<<16) +
  1775. ((unsigned long)iobuf[2]<< 8) +
  1776. ((unsigned long)iobuf[3]);
  1777. dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
  1778. ((unsigned long)iobuf[5]<<16) +
  1779. ((unsigned long)iobuf[6]<< 8) +
  1780. ((unsigned long)iobuf[7]);
  1781. #ifdef CONFIG_LBA48
  1782. dev_desc->lba48 = 0; /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
  1783. #endif
  1784. return;
  1785. }
  1786. /*
  1787. * atapi_read:
  1788. * we transfer only one block per command, since the multiple DRQ per
  1789. * command is not yet implemented
  1790. */
  1791. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1792. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1793. #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
  1794. ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  1795. {
  1796. ulong n = 0;
  1797. unsigned char ccb[12]; /* Command descriptor block */
  1798. ulong cnt;
  1799. debug ("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1800. device, blknr, blkcnt, (ulong)buffer);
  1801. do {
  1802. if (blkcnt>ATAPI_READ_MAX_BLOCK) {
  1803. cnt=ATAPI_READ_MAX_BLOCK;
  1804. } else {
  1805. cnt=blkcnt;
  1806. }
  1807. ccb[0]=ATAPI_CMD_READ_12;
  1808. ccb[1]=0; /* reserved */
  1809. ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
  1810. ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
  1811. ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
  1812. ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
  1813. ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
  1814. ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
  1815. ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
  1816. ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
  1817. ccb[10]=0; /* reserved */
  1818. ccb[11]=0; /* reserved */
  1819. if (atapi_issue_autoreq(device,ccb,12,
  1820. (unsigned char *)buffer,
  1821. cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
  1822. return (n);
  1823. }
  1824. n+=cnt;
  1825. blkcnt-=cnt;
  1826. blknr+=cnt;
  1827. buffer+=(cnt*ATAPI_READ_BLOCK_SIZE);
  1828. } while (blkcnt > 0);
  1829. return (n);
  1830. }
  1831. /* ------------------------------------------------------------------------- */
  1832. #endif /* CONFIG_ATAPI */
  1833. U_BOOT_CMD(
  1834. ide, 5, 1, do_ide,
  1835. "ide - IDE sub-system\n",
  1836. "reset - reset IDE controller\n"
  1837. "ide info - show available IDE devices\n"
  1838. "ide device [dev] - show or set current device\n"
  1839. "ide part [dev] - print partition table of one or all IDE devices\n"
  1840. "ide read addr blk# cnt\n"
  1841. "ide write addr blk# cnt - read/write `cnt'"
  1842. " blocks starting at block `blk#'\n"
  1843. " to/from memory address `addr'\n"
  1844. );
  1845. U_BOOT_CMD(
  1846. diskboot, 3, 1, do_diskboot,
  1847. "diskboot- boot from IDE device\n",
  1848. "loadAddr dev:part\n"
  1849. );