cmd_ide.c 47 KB

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