cmd_ide.c 50 KB

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