cmd_ide.c 51 KB

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