cmd_ide.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. /*
  2. * (C) Copyright 2000-2011
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * IDE support
  9. */
  10. #include <common.h>
  11. #include <config.h>
  12. #include <watchdog.h>
  13. #include <command.h>
  14. #include <image.h>
  15. #include <asm/byteorder.h>
  16. #include <asm/io.h>
  17. #if defined(CONFIG_IDE_8xx_DIRECT) || defined(CONFIG_IDE_PCMCIA)
  18. # include <pcmcia.h>
  19. #endif
  20. #include <ide.h>
  21. #include <ata.h>
  22. #ifdef CONFIG_STATUS_LED
  23. # include <status_led.h>
  24. #endif
  25. #ifdef __PPC__
  26. # define EIEIO __asm__ volatile ("eieio")
  27. # define SYNC __asm__ volatile ("sync")
  28. #else
  29. # define EIEIO /* nothing */
  30. # define SYNC /* nothing */
  31. #endif
  32. /* ------------------------------------------------------------------------- */
  33. /* Current I/O Device */
  34. static int curr_device = -1;
  35. /* Current offset for IDE0 / IDE1 bus access */
  36. ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
  37. #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
  38. CONFIG_SYS_ATA_IDE0_OFFSET,
  39. #endif
  40. #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
  41. CONFIG_SYS_ATA_IDE1_OFFSET,
  42. #endif
  43. };
  44. static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
  45. block_dev_desc_t ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
  46. /* ------------------------------------------------------------------------- */
  47. #ifdef CONFIG_IDE_RESET
  48. static void ide_reset (void);
  49. #else
  50. #define ide_reset() /* dummy */
  51. #endif
  52. static void ide_ident (block_dev_desc_t *dev_desc);
  53. static uchar ide_wait (int dev, ulong t);
  54. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  55. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  56. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  57. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
  58. #ifndef CONFIG_SYS_ATA_PORT_ADDR
  59. #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
  60. #endif
  61. #ifdef CONFIG_ATAPI
  62. static void atapi_inquiry(block_dev_desc_t *dev_desc);
  63. static ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt,
  64. void *buffer);
  65. #endif
  66. /* ------------------------------------------------------------------------- */
  67. int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  68. {
  69. int rcode = 0;
  70. switch (argc) {
  71. case 0:
  72. case 1:
  73. return CMD_RET_USAGE;
  74. case 2:
  75. if (strncmp(argv[1], "res", 3) == 0) {
  76. puts("\nReset IDE"
  77. #ifdef CONFIG_IDE_8xx_DIRECT
  78. " on PCMCIA " PCMCIA_SLOT_MSG
  79. #endif
  80. ": ");
  81. ide_init();
  82. return 0;
  83. } else if (strncmp(argv[1], "inf", 3) == 0) {
  84. int i;
  85. putc('\n');
  86. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
  87. if (ide_dev_desc[i].type == DEV_TYPE_UNKNOWN)
  88. continue; /* list only known devices */
  89. printf("IDE device %d: ", i);
  90. dev_print(&ide_dev_desc[i]);
  91. }
  92. return 0;
  93. } else if (strncmp(argv[1], "dev", 3) == 0) {
  94. if ((curr_device < 0)
  95. || (curr_device >= CONFIG_SYS_IDE_MAXDEVICE)) {
  96. puts("\nno IDE devices available\n");
  97. return 1;
  98. }
  99. printf("\nIDE device %d: ", curr_device);
  100. dev_print(&ide_dev_desc[curr_device]);
  101. return 0;
  102. } else if (strncmp(argv[1], "part", 4) == 0) {
  103. int dev, ok;
  104. for (ok = 0, dev = 0;
  105. dev < CONFIG_SYS_IDE_MAXDEVICE;
  106. ++dev) {
  107. if (ide_dev_desc[dev].part_type !=
  108. PART_TYPE_UNKNOWN) {
  109. ++ok;
  110. if (dev)
  111. putc('\n');
  112. print_part(&ide_dev_desc[dev]);
  113. }
  114. }
  115. if (!ok) {
  116. puts("\nno IDE devices available\n");
  117. rcode++;
  118. }
  119. return rcode;
  120. }
  121. return CMD_RET_USAGE;
  122. case 3:
  123. if (strncmp(argv[1], "dev", 3) == 0) {
  124. int dev = (int) simple_strtoul(argv[2], NULL, 10);
  125. printf("\nIDE device %d: ", dev);
  126. if (dev >= CONFIG_SYS_IDE_MAXDEVICE) {
  127. puts("unknown device\n");
  128. return 1;
  129. }
  130. dev_print(&ide_dev_desc[dev]);
  131. /*ide_print (dev); */
  132. if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
  133. return 1;
  134. curr_device = dev;
  135. puts("... is now current device\n");
  136. return 0;
  137. } else if (strncmp(argv[1], "part", 4) == 0) {
  138. int dev = (int) simple_strtoul(argv[2], NULL, 10);
  139. if (ide_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
  140. print_part(&ide_dev_desc[dev]);
  141. } else {
  142. printf("\nIDE device %d not available\n",
  143. dev);
  144. rcode = 1;
  145. }
  146. return rcode;
  147. }
  148. return CMD_RET_USAGE;
  149. default:
  150. /* at least 4 args */
  151. if (strcmp(argv[1], "read") == 0) {
  152. ulong addr = simple_strtoul(argv[2], NULL, 16);
  153. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  154. ulong n;
  155. #ifdef CONFIG_SYS_64BIT_LBA
  156. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  157. printf("\nIDE read: device %d block # %lld, count %ld ... ",
  158. curr_device, blk, cnt);
  159. #else
  160. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  161. printf("\nIDE read: device %d block # %ld, count %ld ... ",
  162. curr_device, blk, cnt);
  163. #endif
  164. n = ide_dev_desc[curr_device].block_read(curr_device,
  165. blk, cnt,
  166. (ulong *)addr);
  167. /* flush cache after read */
  168. flush_cache(addr,
  169. cnt * ide_dev_desc[curr_device].blksz);
  170. printf("%ld blocks read: %s\n",
  171. n, (n == cnt) ? "OK" : "ERROR");
  172. if (n == cnt)
  173. return 0;
  174. else
  175. return 1;
  176. } else if (strcmp(argv[1], "write") == 0) {
  177. ulong addr = simple_strtoul(argv[2], NULL, 16);
  178. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  179. ulong n;
  180. #ifdef CONFIG_SYS_64BIT_LBA
  181. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  182. printf("\nIDE write: device %d block # %lld, count %ld ... ",
  183. curr_device, blk, cnt);
  184. #else
  185. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  186. printf("\nIDE write: device %d block # %ld, count %ld ... ",
  187. curr_device, blk, cnt);
  188. #endif
  189. n = ide_write(curr_device, blk, cnt, (ulong *) addr);
  190. printf("%ld blocks written: %s\n",
  191. n, (n == cnt) ? "OK" : "ERROR");
  192. if (n == cnt)
  193. return 0;
  194. else
  195. return 1;
  196. } else {
  197. return CMD_RET_USAGE;
  198. }
  199. return rcode;
  200. }
  201. }
  202. int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  203. {
  204. return common_diskboot(cmdtp, "ide", argc, argv);
  205. }
  206. /* ------------------------------------------------------------------------- */
  207. __weak void ide_led(uchar led, uchar status)
  208. {
  209. #if defined(CONFIG_IDE_LED) && defined(PER8_BASE) /* required by LED_PORT */
  210. static uchar led_buffer; /* Buffer for current LED status */
  211. uchar *led_port = LED_PORT;
  212. if (status) /* switch LED on */
  213. led_buffer |= led;
  214. else /* switch LED off */
  215. led_buffer &= ~led;
  216. *led_port = led_buffer;
  217. #endif
  218. }
  219. #ifndef CONFIG_IDE_LED /* define LED macros, they are not used anyways */
  220. # define DEVICE_LED(x) 0
  221. # define LED_IDE1 1
  222. # define LED_IDE2 2
  223. #endif
  224. /* ------------------------------------------------------------------------- */
  225. __weak void ide_outb(int dev, int port, unsigned char val)
  226. {
  227. debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
  228. dev, port, val,
  229. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  230. #if defined(CONFIG_IDE_AHB)
  231. if (port) {
  232. /* write command */
  233. ide_write_register(dev, port, val);
  234. } else {
  235. /* write data */
  236. outb(val, (ATA_CURR_BASE(dev)));
  237. }
  238. #else
  239. outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  240. #endif
  241. }
  242. __weak unsigned char ide_inb(int dev, int port)
  243. {
  244. uchar val;
  245. #if defined(CONFIG_IDE_AHB)
  246. val = ide_read_register(dev, port);
  247. #else
  248. val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  249. #endif
  250. debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
  251. dev, port,
  252. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
  253. return val;
  254. }
  255. void ide_init(void)
  256. {
  257. unsigned char c;
  258. int i, bus;
  259. #ifdef CONFIG_IDE_8xx_PCCARD
  260. extern int ide_devices_found; /* Initialized in check_ide_device() */
  261. #endif /* CONFIG_IDE_8xx_PCCARD */
  262. #ifdef CONFIG_IDE_PREINIT
  263. WATCHDOG_RESET();
  264. if (ide_preinit()) {
  265. puts("ide_preinit failed\n");
  266. return;
  267. }
  268. #endif /* CONFIG_IDE_PREINIT */
  269. WATCHDOG_RESET();
  270. /*
  271. * Reset the IDE just to be sure.
  272. * Light LED's to show
  273. */
  274. ide_led((LED_IDE1 | LED_IDE2), 1); /* LED's on */
  275. /* ATAPI Drives seems to need a proper IDE Reset */
  276. ide_reset();
  277. #ifdef CONFIG_IDE_INIT_POSTRESET
  278. WATCHDOG_RESET();
  279. if (ide_init_postreset()) {
  280. puts("ide_preinit_postreset failed\n");
  281. return;
  282. }
  283. #endif /* CONFIG_IDE_INIT_POSTRESET */
  284. /*
  285. * Wait for IDE to get ready.
  286. * According to spec, this can take up to 31 seconds!
  287. */
  288. for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
  289. int dev =
  290. bus * (CONFIG_SYS_IDE_MAXDEVICE /
  291. CONFIG_SYS_IDE_MAXBUS);
  292. #ifdef CONFIG_IDE_8xx_PCCARD
  293. /* Skip non-ide devices from probing */
  294. if ((ide_devices_found & (1 << bus)) == 0) {
  295. ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  296. continue;
  297. }
  298. #endif
  299. printf("Bus %d: ", bus);
  300. ide_bus_ok[bus] = 0;
  301. /* Select device
  302. */
  303. udelay(100000); /* 100 ms */
  304. ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  305. udelay(100000); /* 100 ms */
  306. i = 0;
  307. do {
  308. udelay(10000); /* 10 ms */
  309. c = ide_inb(dev, ATA_STATUS);
  310. i++;
  311. if (i > (ATA_RESET_TIME * 100)) {
  312. puts("** Timeout **\n");
  313. /* LED's off */
  314. ide_led((LED_IDE1 | LED_IDE2), 0);
  315. return;
  316. }
  317. if ((i >= 100) && ((i % 100) == 0))
  318. putc('.');
  319. } while (c & ATA_STAT_BUSY);
  320. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  321. puts("not available ");
  322. debug("Status = 0x%02X ", c);
  323. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  324. } else if ((c & ATA_STAT_READY) == 0) {
  325. puts("not available ");
  326. debug("Status = 0x%02X ", c);
  327. #endif
  328. } else {
  329. puts("OK ");
  330. ide_bus_ok[bus] = 1;
  331. }
  332. WATCHDOG_RESET();
  333. }
  334. putc('\n');
  335. ide_led((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  336. curr_device = -1;
  337. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
  338. int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
  339. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  340. ide_dev_desc[i].if_type = IF_TYPE_IDE;
  341. ide_dev_desc[i].dev = i;
  342. ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
  343. ide_dev_desc[i].blksz = 0;
  344. ide_dev_desc[i].log2blksz =
  345. LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
  346. ide_dev_desc[i].lba = 0;
  347. ide_dev_desc[i].block_read = ide_read;
  348. ide_dev_desc[i].block_write = ide_write;
  349. if (!ide_bus_ok[IDE_BUS(i)])
  350. continue;
  351. ide_led(led, 1); /* LED on */
  352. ide_ident(&ide_dev_desc[i]);
  353. ide_led(led, 0); /* LED off */
  354. dev_print(&ide_dev_desc[i]);
  355. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  356. /* initialize partition type */
  357. init_part(&ide_dev_desc[i]);
  358. if (curr_device < 0)
  359. curr_device = i;
  360. }
  361. }
  362. WATCHDOG_RESET();
  363. }
  364. /* ------------------------------------------------------------------------- */
  365. #ifdef CONFIG_PARTITIONS
  366. block_dev_desc_t *ide_get_dev(int dev)
  367. {
  368. return (dev < CONFIG_SYS_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
  369. }
  370. #endif
  371. /* ------------------------------------------------------------------------- */
  372. /* We only need to swap data if we are running on a big endian cpu. */
  373. #if defined(__LITTLE_ENDIAN)
  374. __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
  375. {
  376. ide_input_data(dev, sect_buf, words);
  377. }
  378. #else
  379. __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
  380. {
  381. volatile ushort *pbuf =
  382. (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  383. ushort *dbuf = (ushort *) sect_buf;
  384. debug("in input swap data base for read is %lx\n",
  385. (unsigned long) pbuf);
  386. while (words--) {
  387. #ifdef __MIPS__
  388. *dbuf++ = swab16p((u16 *) pbuf);
  389. *dbuf++ = swab16p((u16 *) pbuf);
  390. #else
  391. *dbuf++ = ld_le16(pbuf);
  392. *dbuf++ = ld_le16(pbuf);
  393. #endif /* !MIPS */
  394. }
  395. }
  396. #endif /* __LITTLE_ENDIAN */
  397. #if defined(CONFIG_IDE_SWAP_IO)
  398. __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
  399. {
  400. ushort *dbuf;
  401. volatile ushort *pbuf;
  402. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  403. dbuf = (ushort *) sect_buf;
  404. while (words--) {
  405. EIEIO;
  406. *pbuf = *dbuf++;
  407. EIEIO;
  408. *pbuf = *dbuf++;
  409. }
  410. }
  411. #else /* ! CONFIG_IDE_SWAP_IO */
  412. __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
  413. {
  414. #if defined(CONFIG_IDE_AHB)
  415. ide_write_data(dev, sect_buf, words);
  416. #else
  417. outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
  418. #endif
  419. }
  420. #endif /* CONFIG_IDE_SWAP_IO */
  421. #if defined(CONFIG_IDE_SWAP_IO)
  422. __weak void ide_input_data(int dev, ulong *sect_buf, int words)
  423. {
  424. ushort *dbuf;
  425. volatile ushort *pbuf;
  426. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  427. dbuf = (ushort *) sect_buf;
  428. debug("in input data base for read is %lx\n", (unsigned long) pbuf);
  429. while (words--) {
  430. EIEIO;
  431. *dbuf++ = *pbuf;
  432. EIEIO;
  433. *dbuf++ = *pbuf;
  434. }
  435. }
  436. #else /* ! CONFIG_IDE_SWAP_IO */
  437. __weak void ide_input_data(int dev, ulong *sect_buf, int words)
  438. {
  439. #if defined(CONFIG_IDE_AHB)
  440. ide_read_data(dev, sect_buf, words);
  441. #else
  442. insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
  443. #endif
  444. }
  445. #endif /* CONFIG_IDE_SWAP_IO */
  446. /* -------------------------------------------------------------------------
  447. */
  448. static void ide_ident(block_dev_desc_t *dev_desc)
  449. {
  450. unsigned char c;
  451. hd_driveid_t iop;
  452. #ifdef CONFIG_ATAPI
  453. int retries = 0;
  454. #endif
  455. int device;
  456. device = dev_desc->dev;
  457. printf(" Device %d: ", device);
  458. ide_led(DEVICE_LED(device), 1); /* LED on */
  459. /* Select device
  460. */
  461. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  462. dev_desc->if_type = IF_TYPE_IDE;
  463. #ifdef CONFIG_ATAPI
  464. retries = 0;
  465. /* Warning: This will be tricky to read */
  466. while (retries <= 1) {
  467. /* check signature */
  468. if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
  469. (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
  470. (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
  471. (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
  472. /* ATAPI Signature found */
  473. dev_desc->if_type = IF_TYPE_ATAPI;
  474. /*
  475. * Start Ident Command
  476. */
  477. ide_outb(device, ATA_COMMAND, ATAPI_CMD_IDENT);
  478. /*
  479. * Wait for completion - ATAPI devices need more time
  480. * to become ready
  481. */
  482. c = ide_wait(device, ATAPI_TIME_OUT);
  483. } else
  484. #endif
  485. {
  486. /*
  487. * Start Ident Command
  488. */
  489. ide_outb(device, ATA_COMMAND, ATA_CMD_IDENT);
  490. /*
  491. * Wait for completion
  492. */
  493. c = ide_wait(device, IDE_TIME_OUT);
  494. }
  495. ide_led(DEVICE_LED(device), 0); /* LED off */
  496. if (((c & ATA_STAT_DRQ) == 0) ||
  497. ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
  498. #ifdef CONFIG_ATAPI
  499. {
  500. /*
  501. * Need to soft reset the device
  502. * in case it's an ATAPI...
  503. */
  504. debug("Retrying...\n");
  505. ide_outb(device, ATA_DEV_HD,
  506. ATA_LBA | ATA_DEVICE(device));
  507. udelay(100000);
  508. ide_outb(device, ATA_COMMAND, 0x08);
  509. udelay(500000); /* 500 ms */
  510. }
  511. /*
  512. * Select device
  513. */
  514. ide_outb(device, ATA_DEV_HD,
  515. ATA_LBA | ATA_DEVICE(device));
  516. retries++;
  517. #else
  518. return;
  519. #endif
  520. }
  521. #ifdef CONFIG_ATAPI
  522. else
  523. break;
  524. } /* see above - ugly to read */
  525. if (retries == 2) /* Not found */
  526. return;
  527. #endif
  528. ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
  529. ident_cpy((unsigned char *) dev_desc->revision, iop.fw_rev,
  530. sizeof(dev_desc->revision));
  531. ident_cpy((unsigned char *) dev_desc->vendor, iop.model,
  532. sizeof(dev_desc->vendor));
  533. ident_cpy((unsigned char *) dev_desc->product, iop.serial_no,
  534. sizeof(dev_desc->product));
  535. #ifdef __LITTLE_ENDIAN
  536. /*
  537. * firmware revision, model, and serial number have Big Endian Byte
  538. * order in Word. Convert all three to little endian.
  539. *
  540. * See CF+ and CompactFlash Specification Revision 2.0:
  541. * 6.2.1.6: Identify Drive, Table 39 for more details
  542. */
  543. strswab(dev_desc->revision);
  544. strswab(dev_desc->vendor);
  545. strswab(dev_desc->product);
  546. #endif /* __LITTLE_ENDIAN */
  547. if ((iop.config & 0x0080) == 0x0080)
  548. dev_desc->removable = 1;
  549. else
  550. dev_desc->removable = 0;
  551. #ifdef CONFIG_ATAPI
  552. if (dev_desc->if_type == IF_TYPE_ATAPI) {
  553. atapi_inquiry(dev_desc);
  554. return;
  555. }
  556. #endif /* CONFIG_ATAPI */
  557. #ifdef __BIG_ENDIAN
  558. /* swap shorts */
  559. dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
  560. #else /* ! __BIG_ENDIAN */
  561. /*
  562. * do not swap shorts on little endian
  563. *
  564. * See CF+ and CompactFlash Specification Revision 2.0:
  565. * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
  566. */
  567. dev_desc->lba = iop.lba_capacity;
  568. #endif /* __BIG_ENDIAN */
  569. #ifdef CONFIG_LBA48
  570. if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
  571. dev_desc->lba48 = 1;
  572. dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
  573. ((unsigned long long) iop.lba48_capacity[1] << 16) |
  574. ((unsigned long long) iop.lba48_capacity[2] << 32) |
  575. ((unsigned long long) iop.lba48_capacity[3] << 48);
  576. } else {
  577. dev_desc->lba48 = 0;
  578. }
  579. #endif /* CONFIG_LBA48 */
  580. /* assuming HD */
  581. dev_desc->type = DEV_TYPE_HARDDISK;
  582. dev_desc->blksz = ATA_BLOCKSIZE;
  583. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  584. dev_desc->lun = 0; /* just to fill something in... */
  585. #if 0 /* only used to test the powersaving mode,
  586. * if enabled, the drive goes after 5 sec
  587. * in standby mode */
  588. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  589. c = ide_wait(device, IDE_TIME_OUT);
  590. ide_outb(device, ATA_SECT_CNT, 1);
  591. ide_outb(device, ATA_LBA_LOW, 0);
  592. ide_outb(device, ATA_LBA_MID, 0);
  593. ide_outb(device, ATA_LBA_HIGH, 0);
  594. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  595. ide_outb(device, ATA_COMMAND, 0xe3);
  596. udelay(50);
  597. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  598. #endif
  599. }
  600. /* ------------------------------------------------------------------------- */
  601. ulong ide_read(int device, lbaint_t blknr, lbaint_t blkcnt, void *buffer)
  602. {
  603. ulong n = 0;
  604. unsigned char c;
  605. unsigned char pwrsave = 0; /* power save */
  606. #ifdef CONFIG_LBA48
  607. unsigned char lba48 = 0;
  608. if (blknr & 0x0000fffff0000000ULL) {
  609. /* more than 28 bits used, use 48bit mode */
  610. lba48 = 1;
  611. }
  612. #endif
  613. debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
  614. device, blknr, blkcnt, (ulong) buffer);
  615. ide_led(DEVICE_LED(device), 1); /* LED on */
  616. /* Select device
  617. */
  618. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  619. c = ide_wait(device, IDE_TIME_OUT);
  620. if (c & ATA_STAT_BUSY) {
  621. printf("IDE read: device %d not ready\n", device);
  622. goto IDE_READ_E;
  623. }
  624. /* first check if the drive is in Powersaving mode, if yes,
  625. * increase the timeout value */
  626. ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_PWR);
  627. udelay(50);
  628. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  629. if (c & ATA_STAT_BUSY) {
  630. printf("IDE read: device %d not ready\n", device);
  631. goto IDE_READ_E;
  632. }
  633. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  634. printf("No Powersaving mode %X\n", c);
  635. } else {
  636. c = ide_inb(device, ATA_SECT_CNT);
  637. debug("Powersaving %02X\n", c);
  638. if (c == 0)
  639. pwrsave = 1;
  640. }
  641. while (blkcnt-- > 0) {
  642. c = ide_wait(device, IDE_TIME_OUT);
  643. if (c & ATA_STAT_BUSY) {
  644. printf("IDE read: device %d not ready\n", device);
  645. break;
  646. }
  647. #ifdef CONFIG_LBA48
  648. if (lba48) {
  649. /* write high bits */
  650. ide_outb(device, ATA_SECT_CNT, 0);
  651. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  652. #ifdef CONFIG_SYS_64BIT_LBA
  653. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  654. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  655. #else
  656. ide_outb(device, ATA_LBA_MID, 0);
  657. ide_outb(device, ATA_LBA_HIGH, 0);
  658. #endif
  659. }
  660. #endif
  661. ide_outb(device, ATA_SECT_CNT, 1);
  662. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  663. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  664. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  665. #ifdef CONFIG_LBA48
  666. if (lba48) {
  667. ide_outb(device, ATA_DEV_HD,
  668. ATA_LBA | ATA_DEVICE(device));
  669. ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
  670. } else
  671. #endif
  672. {
  673. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  674. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  675. ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
  676. }
  677. udelay(50);
  678. if (pwrsave) {
  679. /* may take up to 4 sec */
  680. c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
  681. pwrsave = 0;
  682. } else {
  683. /* can't take over 500 ms */
  684. c = ide_wait(device, IDE_TIME_OUT);
  685. }
  686. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  687. ATA_STAT_DRQ) {
  688. printf("Error (no IRQ) dev %d blk " LBAF ": status "
  689. "%#02x\n", device, blknr, c);
  690. break;
  691. }
  692. ide_input_data(device, buffer, ATA_SECTORWORDS);
  693. (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
  694. ++n;
  695. ++blknr;
  696. buffer += ATA_BLOCKSIZE;
  697. }
  698. IDE_READ_E:
  699. ide_led(DEVICE_LED(device), 0); /* LED off */
  700. return (n);
  701. }
  702. /* ------------------------------------------------------------------------- */
  703. ulong ide_write(int device, lbaint_t blknr, lbaint_t blkcnt, const void *buffer)
  704. {
  705. ulong n = 0;
  706. unsigned char c;
  707. #ifdef CONFIG_LBA48
  708. unsigned char lba48 = 0;
  709. if (blknr & 0x0000fffff0000000ULL) {
  710. /* more than 28 bits used, use 48bit mode */
  711. lba48 = 1;
  712. }
  713. #endif
  714. ide_led(DEVICE_LED(device), 1); /* LED on */
  715. /* Select device
  716. */
  717. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  718. while (blkcnt-- > 0) {
  719. c = ide_wait(device, IDE_TIME_OUT);
  720. if (c & ATA_STAT_BUSY) {
  721. printf("IDE read: device %d not ready\n", device);
  722. goto WR_OUT;
  723. }
  724. #ifdef CONFIG_LBA48
  725. if (lba48) {
  726. /* write high bits */
  727. ide_outb(device, ATA_SECT_CNT, 0);
  728. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  729. #ifdef CONFIG_SYS_64BIT_LBA
  730. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  731. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  732. #else
  733. ide_outb(device, ATA_LBA_MID, 0);
  734. ide_outb(device, ATA_LBA_HIGH, 0);
  735. #endif
  736. }
  737. #endif
  738. ide_outb(device, ATA_SECT_CNT, 1);
  739. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  740. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  741. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  742. #ifdef CONFIG_LBA48
  743. if (lba48) {
  744. ide_outb(device, ATA_DEV_HD,
  745. ATA_LBA | ATA_DEVICE(device));
  746. ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
  747. } else
  748. #endif
  749. {
  750. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  751. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  752. ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
  753. }
  754. udelay(50);
  755. /* can't take over 500 ms */
  756. c = ide_wait(device, IDE_TIME_OUT);
  757. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  758. ATA_STAT_DRQ) {
  759. printf("Error (no IRQ) dev %d blk " LBAF ": status "
  760. "%#02x\n", device, blknr, c);
  761. goto WR_OUT;
  762. }
  763. ide_output_data(device, buffer, ATA_SECTORWORDS);
  764. c = ide_inb(device, ATA_STATUS); /* clear IRQ */
  765. ++n;
  766. ++blknr;
  767. buffer += ATA_BLOCKSIZE;
  768. }
  769. WR_OUT:
  770. ide_led(DEVICE_LED(device), 0); /* LED off */
  771. return (n);
  772. }
  773. /* ------------------------------------------------------------------------- */
  774. /*
  775. * copy src to dest, skipping leading and trailing blanks and null
  776. * terminate the string
  777. * "len" is the size of available memory including the terminating '\0'
  778. */
  779. static void ident_cpy(unsigned char *dst, unsigned char *src,
  780. unsigned int len)
  781. {
  782. unsigned char *end, *last;
  783. last = dst;
  784. end = src + len - 1;
  785. /* reserve space for '\0' */
  786. if (len < 2)
  787. goto OUT;
  788. /* skip leading white space */
  789. while ((*src) && (src < end) && (*src == ' '))
  790. ++src;
  791. /* copy string, omitting trailing white space */
  792. while ((*src) && (src < end)) {
  793. *dst++ = *src;
  794. if (*src++ != ' ')
  795. last = dst;
  796. }
  797. OUT:
  798. *last = '\0';
  799. }
  800. /* ------------------------------------------------------------------------- */
  801. /*
  802. * Wait until Busy bit is off, or timeout (in ms)
  803. * Return last status
  804. */
  805. static uchar ide_wait(int dev, ulong t)
  806. {
  807. ulong delay = 10 * t; /* poll every 100 us */
  808. uchar c;
  809. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  810. udelay(100);
  811. if (delay-- == 0)
  812. break;
  813. }
  814. return (c);
  815. }
  816. /* ------------------------------------------------------------------------- */
  817. #ifdef CONFIG_IDE_RESET
  818. extern void ide_set_reset(int idereset);
  819. static void ide_reset(void)
  820. {
  821. int i;
  822. curr_device = -1;
  823. for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
  824. ide_bus_ok[i] = 0;
  825. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
  826. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  827. ide_set_reset(1); /* assert reset */
  828. /* the reset signal shall be asserted for et least 25 us */
  829. udelay(25);
  830. WATCHDOG_RESET();
  831. /* de-assert RESET signal */
  832. ide_set_reset(0);
  833. /* wait 250 ms */
  834. for (i = 0; i < 250; ++i)
  835. udelay(1000);
  836. }
  837. #endif /* CONFIG_IDE_RESET */
  838. /* ------------------------------------------------------------------------- */
  839. #if defined(CONFIG_OF_IDE_FIXUP)
  840. int ide_device_present(int dev)
  841. {
  842. if (dev >= CONFIG_SYS_IDE_MAXBUS)
  843. return 0;
  844. return (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1);
  845. }
  846. #endif
  847. /* ------------------------------------------------------------------------- */
  848. #ifdef CONFIG_ATAPI
  849. /****************************************************************************
  850. * ATAPI Support
  851. */
  852. #if defined(CONFIG_IDE_SWAP_IO)
  853. /* since ATAPI may use commands with not 4 bytes alligned length
  854. * we have our own transfer functions, 2 bytes alligned */
  855. __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  856. {
  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 output data shorts base for read is %lx\n",
  862. (unsigned long) pbuf);
  863. while (shorts--) {
  864. EIEIO;
  865. *pbuf = *dbuf++;
  866. }
  867. }
  868. __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  869. {
  870. ushort *dbuf;
  871. volatile ushort *pbuf;
  872. pbuf = (ushort *) (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  873. dbuf = (ushort *) sect_buf;
  874. debug("in input data shorts base for read is %lx\n",
  875. (unsigned long) pbuf);
  876. while (shorts--) {
  877. EIEIO;
  878. *dbuf++ = *pbuf;
  879. }
  880. }
  881. #else /* ! CONFIG_IDE_SWAP_IO */
  882. __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  883. {
  884. outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
  885. }
  886. __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  887. {
  888. insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
  889. }
  890. #endif /* CONFIG_IDE_SWAP_IO */
  891. /*
  892. * Wait until (Status & mask) == res, or timeout (in ms)
  893. * Return last status
  894. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  895. * and then they set their DRQ Bit
  896. */
  897. static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
  898. {
  899. ulong delay = 10 * t; /* poll every 100 us */
  900. uchar c;
  901. /* prevents to read the status before valid */
  902. c = ide_inb(dev, ATA_DEV_CTL);
  903. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  904. /* break if error occurs (doesn't make sense to wait more) */
  905. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
  906. break;
  907. udelay(100);
  908. if (delay-- == 0)
  909. break;
  910. }
  911. return (c);
  912. }
  913. /*
  914. * issue an atapi command
  915. */
  916. unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
  917. unsigned char *buffer, int buflen)
  918. {
  919. unsigned char c, err, mask, res;
  920. int n;
  921. ide_led(DEVICE_LED(device), 1); /* LED on */
  922. /* Select device
  923. */
  924. mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
  925. res = 0;
  926. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  927. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  928. if ((c & mask) != res) {
  929. printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
  930. c);
  931. err = 0xFF;
  932. goto AI_OUT;
  933. }
  934. /* write taskfile */
  935. ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  936. ide_outb(device, ATA_SECT_CNT, 0);
  937. ide_outb(device, ATA_SECT_NUM, 0);
  938. ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
  939. ide_outb(device, ATA_CYL_HIGH,
  940. (unsigned char) ((buflen >> 8) & 0xFF));
  941. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  942. ide_outb(device, ATA_COMMAND, ATAPI_CMD_PACKET);
  943. udelay(50);
  944. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  945. res = ATA_STAT_DRQ;
  946. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  947. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  948. printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
  949. device, c);
  950. err = 0xFF;
  951. goto AI_OUT;
  952. }
  953. /* write command block */
  954. ide_output_data_shorts(device, (unsigned short *) ccb, ccblen / 2);
  955. /* ATAPI Command written wait for completition */
  956. udelay(5000); /* device must set bsy */
  957. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  958. /*
  959. * if no data wait for DRQ = 0 BSY = 0
  960. * if data wait for DRQ = 1 BSY = 0
  961. */
  962. res = 0;
  963. if (buflen)
  964. res = ATA_STAT_DRQ;
  965. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  966. if ((c & mask) != res) {
  967. if (c & ATA_STAT_ERR) {
  968. err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
  969. debug("atapi_issue 1 returned sense key %X status %02X\n",
  970. err, c);
  971. } else {
  972. printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
  973. ccb[0], c);
  974. err = 0xFF;
  975. }
  976. goto AI_OUT;
  977. }
  978. n = ide_inb(device, ATA_CYL_HIGH);
  979. n <<= 8;
  980. n += ide_inb(device, ATA_CYL_LOW);
  981. if (n > buflen) {
  982. printf("ERROR, transfer bytes %d requested only %d\n", n,
  983. buflen);
  984. err = 0xff;
  985. goto AI_OUT;
  986. }
  987. if ((n == 0) && (buflen < 0)) {
  988. printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
  989. err = 0xff;
  990. goto AI_OUT;
  991. }
  992. if (n != buflen) {
  993. debug("WARNING, transfer bytes %d not equal with requested %d\n",
  994. n, buflen);
  995. }
  996. if (n != 0) { /* data transfer */
  997. debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
  998. /* we transfer shorts */
  999. n >>= 1;
  1000. /* ok now decide if it is an in or output */
  1001. if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
  1002. debug("Write to device\n");
  1003. ide_output_data_shorts(device,
  1004. (unsigned short *) buffer, n);
  1005. } else {
  1006. debug("Read from device @ %p shorts %d\n", buffer, n);
  1007. ide_input_data_shorts(device,
  1008. (unsigned short *) buffer, n);
  1009. }
  1010. }
  1011. udelay(5000); /* seems that some CD ROMs need this... */
  1012. mask = ATA_STAT_BUSY | ATA_STAT_ERR;
  1013. res = 0;
  1014. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  1015. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1016. err = (ide_inb(device, ATA_ERROR_REG) >> 4);
  1017. debug("atapi_issue 2 returned sense key %X status %X\n", err,
  1018. c);
  1019. } else {
  1020. err = 0;
  1021. }
  1022. AI_OUT:
  1023. ide_led(DEVICE_LED(device), 0); /* LED off */
  1024. return (err);
  1025. }
  1026. /*
  1027. * sending the command to atapi_issue. If an status other than good
  1028. * returns, an request_sense will be issued
  1029. */
  1030. #define ATAPI_DRIVE_NOT_READY 100
  1031. #define ATAPI_UNIT_ATTN 10
  1032. unsigned char atapi_issue_autoreq(int device,
  1033. unsigned char *ccb,
  1034. int ccblen,
  1035. unsigned char *buffer, int buflen)
  1036. {
  1037. unsigned char sense_data[18], sense_ccb[12];
  1038. unsigned char res, key, asc, ascq;
  1039. int notready, unitattn;
  1040. unitattn = ATAPI_UNIT_ATTN;
  1041. notready = ATAPI_DRIVE_NOT_READY;
  1042. retry:
  1043. res = atapi_issue(device, ccb, ccblen, buffer, buflen);
  1044. if (res == 0)
  1045. return 0; /* Ok */
  1046. if (res == 0xFF)
  1047. return 0xFF; /* error */
  1048. debug("(auto_req)atapi_issue returned sense key %X\n", res);
  1049. memset(sense_ccb, 0, sizeof(sense_ccb));
  1050. memset(sense_data, 0, sizeof(sense_data));
  1051. sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
  1052. sense_ccb[4] = 18; /* allocation Length */
  1053. res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
  1054. key = (sense_data[2] & 0xF);
  1055. asc = (sense_data[12]);
  1056. ascq = (sense_data[13]);
  1057. debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
  1058. debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1059. sense_data[0], key, asc, ascq);
  1060. if ((key == 0))
  1061. return 0; /* ok device ready */
  1062. if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
  1063. if (unitattn-- > 0) {
  1064. udelay(200 * 1000);
  1065. goto retry;
  1066. }
  1067. printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
  1068. goto error;
  1069. }
  1070. if ((asc == 0x4) && (ascq == 0x1)) {
  1071. /* not ready, but will be ready soon */
  1072. if (notready-- > 0) {
  1073. udelay(200 * 1000);
  1074. goto retry;
  1075. }
  1076. printf("Drive not ready, tried %d times\n",
  1077. ATAPI_DRIVE_NOT_READY);
  1078. goto error;
  1079. }
  1080. if (asc == 0x3a) {
  1081. debug("Media not present\n");
  1082. goto error;
  1083. }
  1084. printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
  1085. ascq);
  1086. error:
  1087. debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
  1088. return (0xFF);
  1089. }
  1090. static void atapi_inquiry(block_dev_desc_t *dev_desc)
  1091. {
  1092. unsigned char ccb[12]; /* Command descriptor block */
  1093. unsigned char iobuf[64]; /* temp buf */
  1094. unsigned char c;
  1095. int device;
  1096. device = dev_desc->dev;
  1097. dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
  1098. dev_desc->block_read = atapi_read;
  1099. memset(ccb, 0, sizeof(ccb));
  1100. memset(iobuf, 0, sizeof(iobuf));
  1101. ccb[0] = ATAPI_CMD_INQUIRY;
  1102. ccb[4] = 40; /* allocation Legnth */
  1103. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 40);
  1104. debug("ATAPI_CMD_INQUIRY returned %x\n", c);
  1105. if (c != 0)
  1106. return;
  1107. /* copy device ident strings */
  1108. ident_cpy((unsigned char *) dev_desc->vendor, &iobuf[8], 8);
  1109. ident_cpy((unsigned char *) dev_desc->product, &iobuf[16], 16);
  1110. ident_cpy((unsigned char *) dev_desc->revision, &iobuf[32], 5);
  1111. dev_desc->lun = 0;
  1112. dev_desc->lba = 0;
  1113. dev_desc->blksz = 0;
  1114. dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
  1115. dev_desc->type = iobuf[0] & 0x1f;
  1116. if ((iobuf[1] & 0x80) == 0x80)
  1117. dev_desc->removable = 1;
  1118. else
  1119. dev_desc->removable = 0;
  1120. memset(ccb, 0, sizeof(ccb));
  1121. memset(iobuf, 0, sizeof(iobuf));
  1122. ccb[0] = ATAPI_CMD_START_STOP;
  1123. ccb[4] = 0x03; /* start */
  1124. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
  1125. debug("ATAPI_CMD_START_STOP returned %x\n", c);
  1126. if (c != 0)
  1127. return;
  1128. memset(ccb, 0, sizeof(ccb));
  1129. memset(iobuf, 0, sizeof(iobuf));
  1130. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 0);
  1131. debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
  1132. if (c != 0)
  1133. return;
  1134. memset(ccb, 0, sizeof(ccb));
  1135. memset(iobuf, 0, sizeof(iobuf));
  1136. ccb[0] = ATAPI_CMD_READ_CAP;
  1137. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *) iobuf, 8);
  1138. debug("ATAPI_CMD_READ_CAP returned %x\n", c);
  1139. if (c != 0)
  1140. return;
  1141. debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1142. iobuf[0], iobuf[1], iobuf[2], iobuf[3],
  1143. iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
  1144. dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
  1145. ((unsigned long) iobuf[1] << 16) +
  1146. ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
  1147. dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
  1148. ((unsigned long) iobuf[5] << 16) +
  1149. ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
  1150. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  1151. #ifdef CONFIG_LBA48
  1152. /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
  1153. dev_desc->lba48 = 0;
  1154. #endif
  1155. return;
  1156. }
  1157. /*
  1158. * atapi_read:
  1159. * we transfer only one block per command, since the multiple DRQ per
  1160. * command is not yet implemented
  1161. */
  1162. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1163. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1164. #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
  1165. ulong atapi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
  1166. {
  1167. ulong n = 0;
  1168. unsigned char ccb[12]; /* Command descriptor block */
  1169. ulong cnt;
  1170. debug("atapi_read dev %d start %lX, blocks " LBAF " buffer at %lX\n",
  1171. device, blknr, blkcnt, (ulong) buffer);
  1172. do {
  1173. if (blkcnt > ATAPI_READ_MAX_BLOCK)
  1174. cnt = ATAPI_READ_MAX_BLOCK;
  1175. else
  1176. cnt = blkcnt;
  1177. ccb[0] = ATAPI_CMD_READ_12;
  1178. ccb[1] = 0; /* reserved */
  1179. ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
  1180. ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
  1181. ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
  1182. ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
  1183. ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
  1184. ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
  1185. ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
  1186. ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
  1187. ccb[10] = 0; /* reserved */
  1188. ccb[11] = 0; /* reserved */
  1189. if (atapi_issue_autoreq(device, ccb, 12,
  1190. (unsigned char *) buffer,
  1191. cnt * ATAPI_READ_BLOCK_SIZE)
  1192. == 0xFF) {
  1193. return (n);
  1194. }
  1195. n += cnt;
  1196. blkcnt -= cnt;
  1197. blknr += cnt;
  1198. buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
  1199. } while (blkcnt > 0);
  1200. return (n);
  1201. }
  1202. /* ------------------------------------------------------------------------- */
  1203. #endif /* CONFIG_ATAPI */
  1204. U_BOOT_CMD(ide, 5, 1, do_ide,
  1205. "IDE sub-system",
  1206. "reset - reset IDE controller\n"
  1207. "ide info - show available IDE devices\n"
  1208. "ide device [dev] - show or set current device\n"
  1209. "ide part [dev] - print partition table of one or all IDE devices\n"
  1210. "ide read addr blk# cnt\n"
  1211. "ide write addr blk# cnt - read/write `cnt'"
  1212. " blocks starting at block `blk#'\n"
  1213. " to/from memory address `addr'");
  1214. U_BOOT_CMD(diskboot, 3, 1, do_diskboot,
  1215. "boot from IDE device", "loadAddr dev:part");