cmd_ide.c 38 KB

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