cmd_ide.c 51 KB

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