cmd_ide.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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. #include <cmd_ide.h>
  42. #include <cmd_disk.h>
  43. #ifdef CONFIG_STATUS_LED
  44. # include <status_led.h>
  45. #endif
  46. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  47. # include <status_led.h>
  48. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  49. #else
  50. # define SHOW_BOOT_PROGRESS(arg)
  51. #endif
  52. #undef IDE_DEBUG
  53. #ifdef IDE_DEBUG
  54. #define PRINTF(fmt,args...) printf (fmt ,##args)
  55. #else
  56. #define PRINTF(fmt,args...)
  57. #endif
  58. #if (CONFIG_COMMANDS & CFG_CMD_IDE)
  59. /* Timings for IDE Interface
  60. *
  61. * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
  62. * 70 165 30 PIO-Mode 0, [ns]
  63. * 4 9 2 [Cycles]
  64. * 50 125 20 PIO-Mode 1, [ns]
  65. * 3 7 2 [Cycles]
  66. * 30 100 15 PIO-Mode 2, [ns]
  67. * 2 6 1 [Cycles]
  68. * 30 80 10 PIO-Mode 3, [ns]
  69. * 2 5 1 [Cycles]
  70. * 25 70 10 PIO-Mode 4, [ns]
  71. * 2 4 1 [Cycles]
  72. */
  73. const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
  74. {
  75. /* Setup Length Hold */
  76. { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
  77. { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
  78. { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
  79. { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
  80. { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
  81. };
  82. static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
  83. #ifndef CFG_PIO_MODE
  84. #define CFG_PIO_MODE 0 /* use a relaxed default */
  85. #endif
  86. static int pio_mode = CFG_PIO_MODE;
  87. /* Make clock cycles and always round up */
  88. #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
  89. /* ------------------------------------------------------------------------- */
  90. /* Current I/O Device */
  91. static int curr_device = -1;
  92. /* Current offset for IDE0 / IDE1 bus access */
  93. ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
  94. #if defined(CFG_ATA_IDE0_OFFSET)
  95. CFG_ATA_IDE0_OFFSET,
  96. #endif
  97. #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
  98. CFG_ATA_IDE1_OFFSET,
  99. #endif
  100. };
  101. #define ATA_CURR_BASE(dev) (CFG_ATA_BASE_ADDR+ide_bus_offset[IDE_BUS(dev)])
  102. static int ide_bus_ok[CFG_IDE_MAXBUS];
  103. static block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
  104. /* ------------------------------------------------------------------------- */
  105. #ifdef CONFIG_IDE_LED
  106. static void ide_led (uchar led, uchar status);
  107. #else
  108. #define ide_led(a,b) /* dummy */
  109. #endif
  110. #ifdef CONFIG_IDE_RESET
  111. static void ide_reset (void);
  112. #else
  113. #define ide_reset() /* dummy */
  114. #endif
  115. static void ide_ident (block_dev_desc_t *dev_desc);
  116. static uchar ide_wait (int dev, ulong t);
  117. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  118. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  119. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  120. static void __inline__ outb(int dev, int port, unsigned char val);
  121. static unsigned char __inline__ inb(int dev, int port);
  122. static void input_swap_data(int dev, ulong *sect_buf, int words);
  123. static void input_data(int dev, ulong *sect_buf, int words);
  124. static void output_data(int dev, ulong *sect_buf, int words);
  125. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
  126. #ifdef CONFIG_ATAPI
  127. static void atapi_inquiry(block_dev_desc_t *dev_desc);
  128. ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer);
  129. #endif
  130. #ifdef CONFIG_IDE_8xx_DIRECT
  131. static void set_pcmcia_timing (int pmode);
  132. #else
  133. #define set_pcmcia_timing(a) /* dummy */
  134. #endif
  135. /* ------------------------------------------------------------------------- */
  136. int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  137. {
  138. int rcode = 0;
  139. switch (argc) {
  140. case 0:
  141. case 1:
  142. printf ("Usage:\n%s\n", cmdtp->usage);
  143. return 1;
  144. case 2:
  145. if (strncmp(argv[1],"res",3) == 0) {
  146. puts ("\nReset IDE"
  147. #ifdef CONFIG_IDE_8xx_DIRECT
  148. " on PCMCIA " PCMCIA_SLOT_MSG
  149. #endif
  150. ": ");
  151. ide_init ();
  152. return 0;
  153. } else if (strncmp(argv[1],"inf",3) == 0) {
  154. int i;
  155. putc ('\n');
  156. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  157. if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
  158. continue; /* list only known devices */
  159. printf ("IDE device %d: ", i);
  160. dev_print(&ide_dev_desc[i]);
  161. }
  162. return 0;
  163. } else if (strncmp(argv[1],"dev",3) == 0) {
  164. if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
  165. puts ("\nno IDE devices available\n");
  166. return 1;
  167. }
  168. printf ("\nIDE device %d: ", curr_device);
  169. dev_print(&ide_dev_desc[curr_device]);
  170. return 0;
  171. } else if (strncmp(argv[1],"part",4) == 0) {
  172. int dev, ok;
  173. for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
  174. if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
  175. ++ok;
  176. if (dev)
  177. putc ('\n');
  178. print_part(&ide_dev_desc[dev]);
  179. }
  180. }
  181. if (!ok) {
  182. puts ("\nno IDE devices available\n");
  183. rcode ++;
  184. }
  185. return rcode;
  186. }
  187. printf ("Usage:\n%s\n", cmdtp->usage);
  188. return 1;
  189. case 3:
  190. if (strncmp(argv[1],"dev",3) == 0) {
  191. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  192. printf ("\nIDE device %d: ", dev);
  193. if (dev >= CFG_IDE_MAXDEVICE) {
  194. puts ("unknown device\n");
  195. return 1;
  196. }
  197. dev_print(&ide_dev_desc[dev]);
  198. /*ide_print (dev);*/
  199. if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
  200. return 1;
  201. }
  202. curr_device = dev;
  203. puts ("... is now current device\n");
  204. return 0;
  205. } else if (strncmp(argv[1],"part",4) == 0) {
  206. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  207. if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
  208. print_part(&ide_dev_desc[dev]);
  209. } else {
  210. printf ("\nIDE device %d not available\n", dev);
  211. rcode = 1;
  212. }
  213. return rcode;
  214. #if 0
  215. } else if (strncmp(argv[1],"pio",4) == 0) {
  216. int mode = (int)simple_strtoul(argv[2], NULL, 10);
  217. if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
  218. puts ("\nSetting ");
  219. pio_mode = mode;
  220. ide_init ();
  221. } else {
  222. printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
  223. mode, IDE_MAX_PIO_MODE);
  224. }
  225. return;
  226. #endif
  227. }
  228. printf ("Usage:\n%s\n", cmdtp->usage);
  229. return 1;
  230. default:
  231. /* at least 4 args */
  232. if (strcmp(argv[1],"read") == 0) {
  233. ulong addr = simple_strtoul(argv[2], NULL, 16);
  234. ulong blk = simple_strtoul(argv[3], NULL, 16);
  235. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  236. ulong n;
  237. printf ("\nIDE read: device %d block # %ld, count %ld ... ",
  238. curr_device, blk, cnt);
  239. n = ide_dev_desc[curr_device].block_read (curr_device,
  240. blk, cnt,
  241. (ulong *)addr);
  242. /* flush cache after read */
  243. flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
  244. printf ("%ld blocks read: %s\n",
  245. n, (n==cnt) ? "OK" : "ERROR");
  246. if (n==cnt) {
  247. return 0;
  248. } else {
  249. return 1;
  250. }
  251. } else if (strcmp(argv[1],"write") == 0) {
  252. ulong addr = simple_strtoul(argv[2], NULL, 16);
  253. ulong blk = simple_strtoul(argv[3], NULL, 16);
  254. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  255. ulong n;
  256. printf ("\nIDE write: device %d block # %ld, count %ld ... ",
  257. curr_device, blk, cnt);
  258. n = ide_write (curr_device, blk, cnt, (ulong *)addr);
  259. printf ("%ld blocks written: %s\n",
  260. n, (n==cnt) ? "OK" : "ERROR");
  261. if (n==cnt) {
  262. return 0;
  263. } else {
  264. return 1;
  265. }
  266. } else {
  267. printf ("Usage:\n%s\n", cmdtp->usage);
  268. rcode = 1;
  269. }
  270. return rcode;
  271. }
  272. }
  273. int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  274. {
  275. char *boot_device = NULL;
  276. char *ep;
  277. int dev, part = 0;
  278. ulong cnt;
  279. ulong addr;
  280. disk_partition_t info;
  281. image_header_t *hdr;
  282. int rcode = 0;
  283. switch (argc) {
  284. case 1:
  285. addr = CFG_LOAD_ADDR;
  286. boot_device = getenv ("bootdevice");
  287. break;
  288. case 2:
  289. addr = simple_strtoul(argv[1], NULL, 16);
  290. boot_device = getenv ("bootdevice");
  291. break;
  292. case 3:
  293. addr = simple_strtoul(argv[1], NULL, 16);
  294. boot_device = argv[2];
  295. break;
  296. default:
  297. printf ("Usage:\n%s\n", cmdtp->usage);
  298. SHOW_BOOT_PROGRESS (-1);
  299. return 1;
  300. }
  301. if (!boot_device) {
  302. puts ("\n** No boot device **\n");
  303. SHOW_BOOT_PROGRESS (-1);
  304. return 1;
  305. }
  306. dev = simple_strtoul(boot_device, &ep, 16);
  307. if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
  308. printf ("\n** Device %d not available\n", dev);
  309. SHOW_BOOT_PROGRESS (-1);
  310. return 1;
  311. }
  312. if (*ep) {
  313. if (*ep != ':') {
  314. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  315. SHOW_BOOT_PROGRESS (-1);
  316. return 1;
  317. }
  318. part = simple_strtoul(++ep, NULL, 16);
  319. }
  320. if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
  321. SHOW_BOOT_PROGRESS (-1);
  322. return 1;
  323. }
  324. if (strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
  325. printf ("\n** Invalid partition type \"%.32s\""
  326. " (expect \"" BOOT_PART_TYPE "\")\n",
  327. info.type);
  328. SHOW_BOOT_PROGRESS (-1);
  329. return 1;
  330. }
  331. printf ("\nLoading from IDE device %d, partition %d: "
  332. "Name: %.32s Type: %.32s\n",
  333. dev, part, info.name, info.type);
  334. PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
  335. info.start, info.size, info.blksz);
  336. if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
  337. printf ("** Read error on %d:%d\n", dev, part);
  338. SHOW_BOOT_PROGRESS (-1);
  339. return 1;
  340. }
  341. hdr = (image_header_t *)addr;
  342. if (ntohl(hdr->ih_magic) == IH_MAGIC) {
  343. print_image_hdr (hdr);
  344. cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
  345. cnt += info.blksz - 1;
  346. cnt /= info.blksz;
  347. cnt -= 1;
  348. } else {
  349. printf("\n** Bad Magic Number **\n");
  350. SHOW_BOOT_PROGRESS (-1);
  351. return 1;
  352. }
  353. if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
  354. (ulong *)(addr+info.blksz)) != cnt) {
  355. printf ("** Read error on %d:%d\n", dev, part);
  356. SHOW_BOOT_PROGRESS (-1);
  357. return 1;
  358. }
  359. /* Loading ok, update default load address */
  360. load_addr = addr;
  361. /* Check if we should attempt an auto-start */
  362. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  363. char *local_args[2];
  364. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  365. local_args[0] = argv[0];
  366. local_args[1] = NULL;
  367. printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
  368. do_bootm (cmdtp, 0, 1, local_args);
  369. rcode = 1;
  370. }
  371. return rcode;
  372. }
  373. /* ------------------------------------------------------------------------- */
  374. void ide_init (void)
  375. {
  376. DECLARE_GLOBAL_DATA_PTR;
  377. #ifdef CONFIG_IDE_8xx_DIRECT
  378. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  379. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  380. #endif
  381. unsigned char c;
  382. int i, bus;
  383. #ifdef CONFIG_IDE_8xx_PCCARD
  384. extern int pcmcia_on (void);
  385. WATCHDOG_RESET();
  386. /* initialize the PCMCIA IDE adapter card */
  387. if (pcmcia_on())
  388. return;
  389. udelay (1000000); /* 1 s */
  390. #endif /* CONFIG_IDE_8xx_PCCARD */
  391. WATCHDOG_RESET();
  392. /* Initialize PIO timing tables */
  393. for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
  394. pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
  395. gd->bus_clk);
  396. pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
  397. gd->bus_clk);
  398. pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
  399. gd->bus_clk);
  400. PRINTF ("PIO Mode %d: setup=%2d ns/%d clk"
  401. " len=%3d ns/%d clk"
  402. " hold=%2d ns/%d clk\n",
  403. i,
  404. pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
  405. pio_config_ns[i].t_length, pio_config_clk[i].t_length,
  406. pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
  407. }
  408. /* Reset the IDE just to be sure.
  409. * Light LED's to show
  410. */
  411. ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
  412. ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
  413. #ifdef CONFIG_IDE_8xx_DIRECT
  414. /* PCMCIA / IDE initialization for common mem space */
  415. pcmp->pcmc_pgcrb = 0;
  416. #endif
  417. /* start in PIO mode 0 - most relaxed timings */
  418. pio_mode = 0;
  419. set_pcmcia_timing (pio_mode);
  420. /*
  421. * Wait for IDE to get ready.
  422. * According to spec, this can take up to 31 seconds!
  423. */
  424. for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
  425. int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
  426. printf ("Bus %d: ", bus);
  427. ide_bus_ok[bus] = 0;
  428. /* Select device
  429. */
  430. udelay (100000); /* 100 ms */
  431. outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  432. udelay (100000); /* 100 ms */
  433. i = 0;
  434. do {
  435. udelay (10000); /* 10 ms */
  436. c = inb (dev, ATA_STATUS);
  437. i++;
  438. if (i > (ATA_RESET_TIME * 100)) {
  439. puts ("** Timeout **\n");
  440. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  441. return;
  442. }
  443. if ((i >= 100) && ((i%100)==0)) {
  444. putc ('.');
  445. }
  446. } while (c & ATA_STAT_BUSY);
  447. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  448. puts ("not available ");
  449. PRINTF ("Status = 0x%02X ", c);
  450. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  451. } else if ((c & ATA_STAT_READY) == 0) {
  452. puts ("not available ");
  453. PRINTF ("Status = 0x%02X ", c);
  454. #endif
  455. } else {
  456. puts ("OK ");
  457. ide_bus_ok[bus] = 1;
  458. }
  459. WATCHDOG_RESET();
  460. }
  461. putc ('\n');
  462. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  463. curr_device = -1;
  464. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  465. #ifdef CONFIG_IDE_LED
  466. int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
  467. #endif
  468. ide_dev_desc[i].if_type=IF_TYPE_IDE;
  469. ide_dev_desc[i].dev=i;
  470. ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
  471. ide_dev_desc[i].blksz=0;
  472. ide_dev_desc[i].lba=0;
  473. ide_dev_desc[i].block_read=ide_read;
  474. if (!ide_bus_ok[IDE_BUS(i)])
  475. continue;
  476. ide_led (led, 1); /* LED on */
  477. ide_ident(&ide_dev_desc[i]);
  478. ide_led (led, 0); /* LED off */
  479. dev_print(&ide_dev_desc[i]);
  480. /* ide_print (i); */
  481. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  482. init_part (&ide_dev_desc[i]); /* initialize partition type */
  483. if (curr_device < 0)
  484. curr_device = i;
  485. }
  486. }
  487. WATCHDOG_RESET();
  488. }
  489. /* ------------------------------------------------------------------------- */
  490. block_dev_desc_t * ide_get_dev(int dev)
  491. {
  492. return ((block_dev_desc_t *)&ide_dev_desc[dev]);
  493. }
  494. #ifdef CONFIG_IDE_8xx_DIRECT
  495. static void
  496. set_pcmcia_timing (int pmode)
  497. {
  498. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  499. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  500. ulong timings;
  501. PRINTF ("Set timing for PIO Mode %d\n", pmode);
  502. timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
  503. | PCMCIA_SST(pio_config_clk[pmode].t_setup)
  504. | PCMCIA_SL (pio_config_clk[pmode].t_length)
  505. ;
  506. /* IDE 0
  507. */
  508. pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
  509. pcmp->pcmc_por0 = CFG_PCMCIA_POR0
  510. #if (CFG_PCMCIA_POR0 != 0)
  511. | timings
  512. #endif
  513. ;
  514. PRINTF ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
  515. pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
  516. pcmp->pcmc_por1 = CFG_PCMCIA_POR1
  517. #if (CFG_PCMCIA_POR1 != 0)
  518. | timings
  519. #endif
  520. ;
  521. PRINTF ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
  522. pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
  523. pcmp->pcmc_por2 = CFG_PCMCIA_POR2
  524. #if (CFG_PCMCIA_POR2 != 0)
  525. | timings
  526. #endif
  527. ;
  528. PRINTF ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
  529. pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
  530. pcmp->pcmc_por3 = CFG_PCMCIA_POR3
  531. #if (CFG_PCMCIA_POR3 != 0)
  532. | timings
  533. #endif
  534. ;
  535. PRINTF ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
  536. /* IDE 1
  537. */
  538. pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
  539. pcmp->pcmc_por4 = CFG_PCMCIA_POR4
  540. #if (CFG_PCMCIA_POR4 != 0)
  541. | timings
  542. #endif
  543. ;
  544. PRINTF ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
  545. pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
  546. pcmp->pcmc_por5 = CFG_PCMCIA_POR5
  547. #if (CFG_PCMCIA_POR5 != 0)
  548. | timings
  549. #endif
  550. ;
  551. PRINTF ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
  552. pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
  553. pcmp->pcmc_por6 = CFG_PCMCIA_POR6
  554. #if (CFG_PCMCIA_POR6 != 0)
  555. | timings
  556. #endif
  557. ;
  558. PRINTF ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
  559. pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
  560. pcmp->pcmc_por7 = CFG_PCMCIA_POR7
  561. #if (CFG_PCMCIA_POR7 != 0)
  562. | timings
  563. #endif
  564. ;
  565. PRINTF ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
  566. }
  567. #endif /* CONFIG_IDE_8xx_DIRECT */
  568. /* ------------------------------------------------------------------------- */
  569. static void __inline__
  570. outb(int dev, int port, unsigned char val)
  571. {
  572. /* Ensure I/O operations complete */
  573. __asm__ volatile("eieio");
  574. *((uchar *)(ATA_CURR_BASE(dev)+port)) = val;
  575. #if 0
  576. printf ("OUTB: 0x%08lx <== 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
  577. #endif
  578. }
  579. static unsigned char __inline__
  580. inb(int dev, int port)
  581. {
  582. uchar val;
  583. /* Ensure I/O operations complete */
  584. __asm__ volatile("eieio");
  585. val = *((uchar *)(ATA_CURR_BASE(dev)+port));
  586. #if 0
  587. printf ("INB: 0x%08lx ==> 0x%02x\n", ATA_CURR_BASE(dev)+port, val);
  588. #endif
  589. return (val);
  590. }
  591. __inline__ unsigned ld_le16(const volatile unsigned short *addr)
  592. {
  593. unsigned val;
  594. __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r"(val) : "r"(addr), "m"(*addr));
  595. return val;
  596. }
  597. static void
  598. input_swap_data(int dev, ulong *sect_buf, int words)
  599. {
  600. volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  601. ushort *dbuf = (ushort *)sect_buf;
  602. while (words--) {
  603. *dbuf++ = ld_le16(pbuf);
  604. *dbuf++ = ld_le16(pbuf);
  605. }
  606. }
  607. static void
  608. output_data(int dev, ulong *sect_buf, int words)
  609. {
  610. ushort *dbuf;
  611. volatile ushort *pbuf;
  612. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  613. dbuf = (ushort *)sect_buf;
  614. while (words--) {
  615. __asm__ volatile ("eieio");
  616. *pbuf = *dbuf++;
  617. __asm__ volatile ("eieio");
  618. *pbuf = *dbuf++;
  619. }
  620. }
  621. static void
  622. input_data(int dev, ulong *sect_buf, int words)
  623. {
  624. ushort *dbuf;
  625. volatile ushort *pbuf;
  626. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  627. dbuf = (ushort *)sect_buf;
  628. while (words--) {
  629. __asm__ volatile ("eieio");
  630. *dbuf++ = *pbuf;
  631. __asm__ volatile ("eieio");
  632. *dbuf++ = *pbuf;
  633. }
  634. }
  635. /* -------------------------------------------------------------------------
  636. */
  637. static void ide_ident (block_dev_desc_t *dev_desc)
  638. {
  639. ulong iobuf[ATA_SECTORWORDS];
  640. unsigned char c;
  641. hd_driveid_t *iop = (hd_driveid_t *)iobuf;
  642. #if 0
  643. int mode, cycle_time;
  644. #endif
  645. int device;
  646. device=dev_desc->dev;
  647. printf (" Device %d: ", device);
  648. ide_led (DEVICE_LED(device), 1); /* LED on */
  649. /* Select device
  650. */
  651. outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  652. dev_desc->if_type=IF_TYPE_IDE;
  653. #ifdef CONFIG_ATAPI
  654. /* check signature */
  655. if ((inb(device,ATA_SECT_CNT)==0x01) &&
  656. (inb(device,ATA_SECT_NUM)==0x01) &&
  657. (inb(device,ATA_CYL_LOW)==0x14) &&
  658. (inb(device,ATA_CYL_HIGH)==0xEB)) {
  659. /* ATAPI Signature found */
  660. dev_desc->if_type=IF_TYPE_ATAPI;
  661. /* Start Ident Command
  662. */
  663. outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
  664. /*
  665. * Wait for completion - ATAPI devices need more time
  666. * to become ready
  667. */
  668. c = ide_wait (device, ATAPI_TIME_OUT);
  669. }
  670. else
  671. #endif
  672. {
  673. /* Start Ident Command
  674. */
  675. outb (device, ATA_COMMAND, ATA_CMD_IDENT);
  676. /* Wait for completion
  677. */
  678. c = ide_wait (device, IDE_TIME_OUT);
  679. }
  680. ide_led (DEVICE_LED(device), 0); /* LED off */
  681. if (((c & ATA_STAT_DRQ) == 0) ||
  682. ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
  683. dev_desc->type=DEV_TYPE_UNKNOWN;
  684. return;
  685. }
  686. input_swap_data (device, iobuf, ATA_SECTORWORDS);
  687. ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
  688. ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
  689. ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
  690. if ((iop->config & 0x0080)==0x0080)
  691. dev_desc->removable = 1;
  692. else
  693. dev_desc->removable = 0;
  694. #if 0
  695. /*
  696. * Drive PIO mode autoselection
  697. */
  698. mode = iop->tPIO;
  699. printf ("tPIO = 0x%02x = %d\n",mode, mode);
  700. if (mode > 2) { /* 2 is maximum allowed tPIO value */
  701. mode = 2;
  702. PRINTF ("Override tPIO -> 2\n");
  703. }
  704. if (iop->field_valid & 2) { /* drive implements ATA2? */
  705. PRINTF ("Drive implements ATA2\n");
  706. if (iop->capability & 8) { /* drive supports use_iordy? */
  707. cycle_time = iop->eide_pio_iordy;
  708. } else {
  709. cycle_time = iop->eide_pio;
  710. }
  711. PRINTF ("cycle time = %d\n", cycle_time);
  712. mode = 4;
  713. if (cycle_time > 120) mode = 3; /* 120 ns for PIO mode 4 */
  714. if (cycle_time > 180) mode = 2; /* 180 ns for PIO mode 3 */
  715. if (cycle_time > 240) mode = 1; /* 240 ns for PIO mode 4 */
  716. if (cycle_time > 383) mode = 0; /* 383 ns for PIO mode 4 */
  717. }
  718. printf ("PIO mode to use: PIO %d\n", mode);
  719. #endif /* 0 */
  720. #ifdef CONFIG_ATAPI
  721. if (dev_desc->if_type==IF_TYPE_ATAPI) {
  722. atapi_inquiry(dev_desc);
  723. return;
  724. }
  725. #endif /* CONFIG_ATAPI */
  726. /* swap shorts */
  727. dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
  728. /* assuming HD */
  729. dev_desc->type=DEV_TYPE_HARDDISK;
  730. dev_desc->blksz=ATA_BLOCKSIZE;
  731. dev_desc->lun=0; /* just to fill something in... */
  732. #if 0 /* only used to test the powersaving mode,
  733. * if enabled, the drive goes after 5 sec
  734. * in standby mode */
  735. outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  736. c = ide_wait (device, IDE_TIME_OUT);
  737. outb (device, ATA_SECT_CNT, 1);
  738. outb (device, ATA_LBA_LOW, 0);
  739. outb (device, ATA_LBA_MID, 0);
  740. outb (device, ATA_LBA_HIGH, 0);
  741. outb (device, ATA_DEV_HD, ATA_LBA |
  742. ATA_DEVICE(device));
  743. outb (device, ATA_COMMAND, 0xe3);
  744. udelay (50);
  745. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  746. #endif
  747. }
  748. /* ------------------------------------------------------------------------- */
  749. ulong ide_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  750. {
  751. ulong n = 0;
  752. unsigned char c;
  753. unsigned char pwrsave=0; /* power save */
  754. PRINTF ("ide_read dev %d start %lX, blocks %lX buffer at %lX\n",
  755. device, blknr, blkcnt, (ulong)buffer);
  756. ide_led (DEVICE_LED(device), 1); /* LED on */
  757. /* Select device
  758. */
  759. outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  760. c = ide_wait (device, IDE_TIME_OUT);
  761. if (c & ATA_STAT_BUSY) {
  762. printf ("IDE read: device %d not ready\n", device);
  763. goto IDE_READ_E;
  764. }
  765. /* first check if the drive is in Powersaving mode, if yes,
  766. * increase the timeout value */
  767. outb (device, ATA_COMMAND, ATA_CMD_CHK_PWR);
  768. udelay (50);
  769. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  770. if (c & ATA_STAT_BUSY) {
  771. printf ("IDE read: device %d not ready\n", device);
  772. goto IDE_READ_E;
  773. }
  774. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  775. printf ("No Powersaving mode %X\n", c);
  776. } else {
  777. c = inb(device,ATA_SECT_CNT);
  778. PRINTF("Powersaving %02X\n",c);
  779. if(c==0)
  780. pwrsave=1;
  781. }
  782. while (blkcnt-- > 0) {
  783. c = ide_wait (device, IDE_TIME_OUT);
  784. if (c & ATA_STAT_BUSY) {
  785. printf ("IDE read: device %d not ready\n", device);
  786. break;
  787. }
  788. outb (device, ATA_SECT_CNT, 1);
  789. outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  790. outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  791. outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  792. outb (device, ATA_DEV_HD, ATA_LBA |
  793. ATA_DEVICE(device) |
  794. ((blknr >> 24) & 0xF) );
  795. outb (device, ATA_COMMAND, ATA_CMD_READ);
  796. udelay (50);
  797. if(pwrsave) {
  798. c = ide_wait (device, IDE_SPIN_UP_TIME_OUT); /* may take up to 4 sec */
  799. pwrsave=0;
  800. } else {
  801. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  802. }
  803. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  804. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  805. device, blknr, c);
  806. break;
  807. }
  808. input_data (device, buffer, ATA_SECTORWORDS);
  809. (void) inb (device, ATA_STATUS); /* clear IRQ */
  810. ++n;
  811. ++blknr;
  812. buffer += ATA_SECTORWORDS;
  813. }
  814. IDE_READ_E:
  815. ide_led (DEVICE_LED(device), 0); /* LED off */
  816. return (n);
  817. }
  818. /* ------------------------------------------------------------------------- */
  819. ulong ide_write (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  820. {
  821. ulong n = 0;
  822. unsigned char c;
  823. ide_led (DEVICE_LED(device), 1); /* LED on */
  824. /* Select device
  825. */
  826. outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  827. while (blkcnt-- > 0) {
  828. c = ide_wait (device, IDE_TIME_OUT);
  829. if (c & ATA_STAT_BUSY) {
  830. printf ("IDE read: device %d not ready\n", device);
  831. goto WR_OUT;
  832. }
  833. outb (device, ATA_SECT_CNT, 1);
  834. outb (device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  835. outb (device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  836. outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  837. outb (device, ATA_DEV_HD, ATA_LBA |
  838. ATA_DEVICE(device) |
  839. ((blknr >> 24) & 0xF) );
  840. outb (device, ATA_COMMAND, ATA_CMD_WRITE);
  841. udelay (50);
  842. c = ide_wait (device, IDE_TIME_OUT); /* can't take over 500 ms */
  843. if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
  844. printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
  845. device, blknr, c);
  846. goto WR_OUT;
  847. }
  848. output_data (device, buffer, ATA_SECTORWORDS);
  849. c = inb (device, ATA_STATUS); /* clear IRQ */
  850. ++n;
  851. ++blknr;
  852. buffer += ATA_SECTORWORDS;
  853. }
  854. WR_OUT:
  855. ide_led (DEVICE_LED(device), 0); /* LED off */
  856. return (n);
  857. }
  858. /* ------------------------------------------------------------------------- */
  859. /*
  860. * copy src to dest, skipping leading and trailing blanks and null
  861. * terminate the string
  862. */
  863. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
  864. {
  865. int start,end;
  866. start=0;
  867. while (start<len) {
  868. if (src[start]!=' ')
  869. break;
  870. start++;
  871. }
  872. end=len-1;
  873. while (end>start) {
  874. if (src[end]!=' ')
  875. break;
  876. end--;
  877. }
  878. for ( ; start<=end; start++) {
  879. *dest++=src[start];
  880. }
  881. *dest='\0';
  882. }
  883. /* ------------------------------------------------------------------------- */
  884. /*
  885. * Wait until Busy bit is off, or timeout (in ms)
  886. * Return last status
  887. */
  888. static uchar ide_wait (int dev, ulong t)
  889. {
  890. ulong delay = 10 * t; /* poll every 100 us */
  891. uchar c;
  892. while ((c = inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  893. udelay (100);
  894. if (delay-- == 0) {
  895. break;
  896. }
  897. }
  898. return (c);
  899. }
  900. /* ------------------------------------------------------------------------- */
  901. #ifdef CONFIG_IDE_RESET
  902. extern void ide_set_reset(int idereset);
  903. static void ide_reset (void)
  904. {
  905. #if defined(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
  906. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  907. #endif
  908. int i;
  909. curr_device = -1;
  910. for (i=0; i<CFG_IDE_MAXBUS; ++i)
  911. ide_bus_ok[i] = 0;
  912. for (i=0; i<CFG_IDE_MAXDEVICE; ++i)
  913. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  914. ide_set_reset (1); /* assert reset */
  915. WATCHDOG_RESET();
  916. #ifdef CFG_PB_12V_ENABLE
  917. immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
  918. immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
  919. immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
  920. immr->im_cpm.cp_pbdir |= CFG_PB_12V_ENABLE;
  921. /* wait 500 ms for the voltage to stabilize
  922. */
  923. for (i=0; i<500; ++i) {
  924. udelay (1000);
  925. }
  926. immr->im_cpm.cp_pbdat |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
  927. #endif /* CFG_PB_12V_ENABLE */
  928. #ifdef CFG_PB_IDE_MOTOR
  929. /* configure IDE Motor voltage monitor pin as input */
  930. immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
  931. immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
  932. immr->im_cpm.cp_pbdir &= ~(CFG_PB_IDE_MOTOR);
  933. /* wait up to 1 s for the motor voltage to stabilize
  934. */
  935. for (i=0; i<1000; ++i) {
  936. if ((immr->im_cpm.cp_pbdat & CFG_PB_IDE_MOTOR) != 0) {
  937. break;
  938. }
  939. udelay (1000);
  940. }
  941. if (i == 1000) { /* Timeout */
  942. printf ("\nWarning: 5V for IDE Motor missing\n");
  943. # ifdef CONFIG_STATUS_LED
  944. # ifdef STATUS_LED_YELLOW
  945. status_led_set (STATUS_LED_YELLOW, STATUS_LED_ON );
  946. # endif
  947. # ifdef STATUS_LED_GREEN
  948. status_led_set (STATUS_LED_GREEN, STATUS_LED_OFF);
  949. # endif
  950. # endif /* CONFIG_STATUS_LED */
  951. }
  952. #endif /* CFG_PB_IDE_MOTOR */
  953. WATCHDOG_RESET();
  954. /* de-assert RESET signal */
  955. ide_set_reset(0);
  956. /* wait 250 ms */
  957. for (i=0; i<250; ++i) {
  958. udelay (1000);
  959. }
  960. }
  961. #endif /* CONFIG_IDE_RESET */
  962. /* ------------------------------------------------------------------------- */
  963. #ifdef CONFIG_IDE_LED
  964. static uchar led_buffer = 0; /* Buffer for current LED status */
  965. static void ide_led (uchar led, uchar status)
  966. {
  967. uchar *led_port = LED_PORT;
  968. if (status) { /* switch LED on */
  969. led_buffer |= led;
  970. } else { /* switch LED off */
  971. led_buffer &= ~led;
  972. }
  973. *led_port = led_buffer;
  974. }
  975. #endif /* CONFIG_IDE_LED */
  976. /* ------------------------------------------------------------------------- */
  977. #ifdef CONFIG_ATAPI
  978. /****************************************************************************
  979. * ATAPI Support
  980. */
  981. #undef ATAPI_DEBUG
  982. #ifdef ATAPI_DEBUG
  983. #define AT_PRINTF(fmt,args...) printf (fmt ,##args)
  984. #else
  985. #define AT_PRINTF(fmt,args...)
  986. #endif
  987. /* since ATAPI may use commands with not 4 bytes alligned length
  988. * we have our own transfer functions, 2 bytes alligned */
  989. static void
  990. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  991. {
  992. ushort *dbuf;
  993. volatile ushort *pbuf;
  994. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  995. dbuf = (ushort *)sect_buf;
  996. while (shorts--) {
  997. __asm__ volatile ("eieio");
  998. *pbuf = *dbuf++;
  999. }
  1000. }
  1001. static void
  1002. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1003. {
  1004. ushort *dbuf;
  1005. volatile ushort *pbuf;
  1006. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1007. dbuf = (ushort *)sect_buf;
  1008. while (shorts--) {
  1009. __asm__ volatile ("eieio");
  1010. *dbuf++ = *pbuf;
  1011. }
  1012. }
  1013. /*
  1014. * Wait until (Status & mask) == res, or timeout (in ms)
  1015. * Return last status
  1016. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  1017. * and then they set their DRQ Bit
  1018. */
  1019. static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
  1020. {
  1021. ulong delay = 10 * t; /* poll every 100 us */
  1022. uchar c;
  1023. c = inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
  1024. while (((c = inb(dev, ATA_STATUS)) & mask)
  1025. != res) {
  1026. /* break if error occurs (doesn't make sense to wait more) */
  1027. if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
  1028. break;
  1029. udelay (100);
  1030. if (delay-- == 0) {
  1031. break;
  1032. }
  1033. }
  1034. return (c);
  1035. }
  1036. /*
  1037. * issue an atapi command
  1038. */
  1039. unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
  1040. {
  1041. unsigned char c,err,mask,res;
  1042. int n;
  1043. ide_led (DEVICE_LED(device), 1); /* LED on */
  1044. /* Select device
  1045. */
  1046. mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
  1047. res = 0;
  1048. outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1049. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1050. if ((c & mask) != res) {
  1051. printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
  1052. err=0xFF;
  1053. goto AI_OUT;
  1054. }
  1055. /* write taskfile */
  1056. outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  1057. outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
  1058. outb (device, ATA_CYL_HIGH, (unsigned char)((buflen<<8) & 0xFF));
  1059. outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1060. outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
  1061. udelay (50);
  1062. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1063. res = ATA_STAT_DRQ;
  1064. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1065. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  1066. printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
  1067. err=0xFF;
  1068. goto AI_OUT;
  1069. }
  1070. output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
  1071. /* ATAPI Command written wait for completition */
  1072. udelay (5000); /* device must set bsy */
  1073. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1074. /* if no data wait for DRQ = 0 BSY = 0
  1075. * if data wait for DRQ = 1 BSY = 0 */
  1076. res=0;
  1077. if(buflen)
  1078. res = ATA_STAT_DRQ;
  1079. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1080. if ((c & mask) != res ) {
  1081. if (c & ATA_STAT_ERR) {
  1082. err=(inb(device,ATA_ERROR_REG))>>4;
  1083. AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
  1084. } else {
  1085. printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
  1086. err=0xFF;
  1087. }
  1088. goto AI_OUT;
  1089. }
  1090. n=inb(device, ATA_CYL_HIGH);
  1091. n<<=8;
  1092. n+=inb(device, ATA_CYL_LOW);
  1093. if(n>buflen) {
  1094. printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
  1095. err=0xff;
  1096. goto AI_OUT;
  1097. }
  1098. if((n==0)&&(buflen<0)) {
  1099. printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
  1100. err=0xff;
  1101. goto AI_OUT;
  1102. }
  1103. if(n!=buflen) {
  1104. AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
  1105. }
  1106. if(n!=0) { /* data transfer */
  1107. AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
  1108. /* we transfer shorts */
  1109. n>>=1;
  1110. /* ok now decide if it is an in or output */
  1111. if ((inb(device, ATA_SECT_CNT)&0x02)==0) {
  1112. AT_PRINTF("Write to device\n");
  1113. output_data_shorts(device,(unsigned short *)buffer,n);
  1114. } else {
  1115. AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
  1116. input_data_shorts(device,(unsigned short *)buffer,n);
  1117. }
  1118. }
  1119. udelay(5000); /* seems that some CD ROMs need this... */
  1120. mask = ATA_STAT_BUSY|ATA_STAT_ERR;
  1121. res=0;
  1122. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1123. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1124. err=(inb(device,ATA_ERROR_REG) >> 4);
  1125. AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
  1126. } else {
  1127. err = 0;
  1128. }
  1129. AI_OUT:
  1130. ide_led (DEVICE_LED(device), 0); /* LED off */
  1131. return (err);
  1132. }
  1133. /*
  1134. * sending the command to atapi_issue. If an status other than good
  1135. * returns, an request_sense will be issued
  1136. */
  1137. #define ATAPI_DRIVE_NOT_READY 100
  1138. #define ATAPI_UNIT_ATTN 10
  1139. unsigned char atapi_issue_autoreq (int device,
  1140. unsigned char* ccb,
  1141. int ccblen,
  1142. unsigned char *buffer,
  1143. int buflen)
  1144. {
  1145. unsigned char sense_data[18],sense_ccb[12];
  1146. unsigned char res,key,asc,ascq;
  1147. int notready,unitattn;
  1148. unitattn=ATAPI_UNIT_ATTN;
  1149. notready=ATAPI_DRIVE_NOT_READY;
  1150. retry:
  1151. res= atapi_issue(device,ccb,ccblen,buffer,buflen);
  1152. if (res==0)
  1153. return (0); /* Ok */
  1154. if (res==0xFF)
  1155. return (0xFF); /* error */
  1156. AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
  1157. memset(sense_ccb,0,sizeof(sense_ccb));
  1158. memset(sense_data,0,sizeof(sense_data));
  1159. sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
  1160. sense_ccb[4]=18; /* allocation Legnth */
  1161. res=atapi_issue(device,sense_ccb,12,sense_data,18);
  1162. key=(sense_data[2]&0xF);
  1163. asc=(sense_data[12]);
  1164. ascq=(sense_data[13]);
  1165. AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
  1166. AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1167. sense_data[0],
  1168. key,
  1169. asc,
  1170. ascq);
  1171. if((key==0))
  1172. return 0; /* ok device ready */
  1173. if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
  1174. if(unitattn-->0) {
  1175. udelay(200*1000);
  1176. goto retry;
  1177. }
  1178. printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
  1179. goto error;
  1180. }
  1181. if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
  1182. if (notready-->0) {
  1183. udelay(200*1000);
  1184. goto retry;
  1185. }
  1186. printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
  1187. goto error;
  1188. }
  1189. if(asc==0x3a) {
  1190. AT_PRINTF("Media not present\n");
  1191. goto error;
  1192. }
  1193. printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1194. error:
  1195. AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1196. return (0xFF);
  1197. }
  1198. static void atapi_inquiry(block_dev_desc_t * dev_desc)
  1199. {
  1200. unsigned char ccb[12]; /* Command descriptor block */
  1201. unsigned char iobuf[64]; /* temp buf */
  1202. unsigned char c;
  1203. int device;
  1204. device=dev_desc->dev;
  1205. dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
  1206. dev_desc->block_read=atapi_read;
  1207. memset(ccb,0,sizeof(ccb));
  1208. memset(iobuf,0,sizeof(iobuf));
  1209. ccb[0]=ATAPI_CMD_INQUIRY;
  1210. ccb[4]=40; /* allocation Legnth */
  1211. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
  1212. AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
  1213. if (c!=0)
  1214. return;
  1215. /* copy device ident strings */
  1216. ident_cpy(dev_desc->vendor,&iobuf[8],8);
  1217. ident_cpy(dev_desc->product,&iobuf[16],16);
  1218. ident_cpy(dev_desc->revision,&iobuf[32],5);
  1219. dev_desc->lun=0;
  1220. dev_desc->lba=0;
  1221. dev_desc->blksz=0;
  1222. dev_desc->type=iobuf[0] & 0x1f;
  1223. if ((iobuf[1]&0x80)==0x80)
  1224. dev_desc->removable = 1;
  1225. else
  1226. dev_desc->removable = 0;
  1227. memset(ccb,0,sizeof(ccb));
  1228. memset(iobuf,0,sizeof(iobuf));
  1229. ccb[0]=ATAPI_CMD_START_STOP;
  1230. ccb[4]=0x03; /* start */
  1231. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1232. AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
  1233. if (c!=0)
  1234. return;
  1235. memset(ccb,0,sizeof(ccb));
  1236. memset(iobuf,0,sizeof(iobuf));
  1237. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1238. AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
  1239. if (c!=0)
  1240. return;
  1241. memset(ccb,0,sizeof(ccb));
  1242. memset(iobuf,0,sizeof(iobuf));
  1243. ccb[0]=ATAPI_CMD_READ_CAP;
  1244. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
  1245. AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
  1246. if (c!=0)
  1247. return;
  1248. AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1249. iobuf[0],iobuf[1],iobuf[2],iobuf[3],
  1250. iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
  1251. dev_desc->lba =((unsigned long)iobuf[0]<<24) +
  1252. ((unsigned long)iobuf[1]<<16) +
  1253. ((unsigned long)iobuf[2]<< 8) +
  1254. ((unsigned long)iobuf[3]);
  1255. dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
  1256. ((unsigned long)iobuf[5]<<16) +
  1257. ((unsigned long)iobuf[6]<< 8) +
  1258. ((unsigned long)iobuf[7]);
  1259. return;
  1260. }
  1261. /*
  1262. * atapi_read:
  1263. * we transfer only one block per command, since the multiple DRQ per
  1264. * command is not yet implemented
  1265. */
  1266. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1267. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1268. #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
  1269. ulong atapi_read (int device, ulong blknr, ulong blkcnt, ulong *buffer)
  1270. {
  1271. ulong n = 0;
  1272. unsigned char ccb[12]; /* Command descriptor block */
  1273. ulong cnt;
  1274. AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1275. device, blknr, blkcnt, (ulong)buffer);
  1276. do {
  1277. if (blkcnt>ATAPI_READ_MAX_BLOCK) {
  1278. cnt=ATAPI_READ_MAX_BLOCK;
  1279. } else {
  1280. cnt=blkcnt;
  1281. }
  1282. ccb[0]=ATAPI_CMD_READ_12;
  1283. ccb[1]=0; /* reserved */
  1284. ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
  1285. ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
  1286. ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
  1287. ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
  1288. ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
  1289. ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
  1290. ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
  1291. ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
  1292. ccb[10]=0; /* reserved */
  1293. ccb[11]=0; /* reserved */
  1294. if (atapi_issue_autoreq(device,ccb,12,
  1295. (unsigned char *)buffer,
  1296. cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
  1297. return (n);
  1298. }
  1299. n+=cnt;
  1300. blkcnt-=cnt;
  1301. blknr+=cnt;
  1302. buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
  1303. } while (blkcnt > 0);
  1304. return (n);
  1305. }
  1306. /* ------------------------------------------------------------------------- */
  1307. #endif /* CONFIG_ATAPI */
  1308. #endif /* CONFIG_COMMANDS & CFG_CMD_IDE */