cmd_ide.c 50 KB

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