cmd_ide.c 51 KB

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