cmd_ide.c 39 KB

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