cmd_ide.c 52 KB

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