cmd_ide.c 51 KB

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