cmd_ide.c 51 KB

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