cmd_ide.c 50 KB

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