cmd_ide.c 44 KB

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