cmd_ide.c 39 KB

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