cmd_ide.c 39 KB

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