cmd_ide.c 47 KB

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