cmd_ide.c 39 KB

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