cmd_ide.c 36 KB

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