cmd_ide.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  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. #ifndef __PPC__
  49. #include <asm/io.h>
  50. #endif
  51. #ifdef CONFIG_IDE_8xx_DIRECT
  52. DECLARE_GLOBAL_DATA_PTR;
  53. #endif
  54. #ifdef __PPC__
  55. # define EIEIO __asm__ volatile ("eieio")
  56. # define SYNC __asm__ volatile ("sync")
  57. #else
  58. # define EIEIO /* nothing */
  59. # define SYNC /* nothing */
  60. #endif
  61. #ifdef CONFIG_IDE_8xx_DIRECT
  62. /* Timings for IDE Interface
  63. *
  64. * SETUP / LENGTH / HOLD - cycles valid for 50 MHz clk
  65. * 70 165 30 PIO-Mode 0, [ns]
  66. * 4 9 2 [Cycles]
  67. * 50 125 20 PIO-Mode 1, [ns]
  68. * 3 7 2 [Cycles]
  69. * 30 100 15 PIO-Mode 2, [ns]
  70. * 2 6 1 [Cycles]
  71. * 30 80 10 PIO-Mode 3, [ns]
  72. * 2 5 1 [Cycles]
  73. * 25 70 10 PIO-Mode 4, [ns]
  74. * 2 4 1 [Cycles]
  75. */
  76. const static pio_config_t pio_config_ns [IDE_MAX_PIO_MODE+1] =
  77. {
  78. /* Setup Length Hold */
  79. { 70, 165, 30 }, /* PIO-Mode 0, [ns] */
  80. { 50, 125, 20 }, /* PIO-Mode 1, [ns] */
  81. { 30, 101, 15 }, /* PIO-Mode 2, [ns] */
  82. { 30, 80, 10 }, /* PIO-Mode 3, [ns] */
  83. { 25, 70, 10 }, /* PIO-Mode 4, [ns] */
  84. };
  85. static pio_config_t pio_config_clk [IDE_MAX_PIO_MODE+1];
  86. #ifndef CFG_PIO_MODE
  87. #define CFG_PIO_MODE 0 /* use a relaxed default */
  88. #endif
  89. static int pio_mode = CFG_PIO_MODE;
  90. /* Make clock cycles and always round up */
  91. #define PCMCIA_MK_CLKS( t, T ) (( (t) * (T) + 999U ) / 1000U )
  92. #endif /* CONFIG_IDE_8xx_DIRECT */
  93. /* ------------------------------------------------------------------------- */
  94. /* Current I/O Device */
  95. static int curr_device = -1;
  96. /* Current offset for IDE0 / IDE1 bus access */
  97. ulong ide_bus_offset[CFG_IDE_MAXBUS] = {
  98. #if defined(CFG_ATA_IDE0_OFFSET)
  99. CFG_ATA_IDE0_OFFSET,
  100. #endif
  101. #if defined(CFG_ATA_IDE1_OFFSET) && (CFG_IDE_MAXBUS > 1)
  102. CFG_ATA_IDE1_OFFSET,
  103. #endif
  104. };
  105. #ifndef CONFIG_AMIGAONEG3SE
  106. static int ide_bus_ok[CFG_IDE_MAXBUS];
  107. #else
  108. static int ide_bus_ok[CFG_IDE_MAXBUS] = {0,};
  109. #endif
  110. block_dev_desc_t ide_dev_desc[CFG_IDE_MAXDEVICE];
  111. /* ------------------------------------------------------------------------- */
  112. #ifdef CONFIG_IDE_LED
  113. #if !defined(CONFIG_KUP4K) && !defined(CONFIG_KUP4X) &&!defined(CONFIG_BMS2003) &&!defined(CONFIG_CPC45)
  114. static void ide_led (uchar led, uchar status);
  115. #else
  116. extern void ide_led (uchar led, uchar status);
  117. #endif
  118. #else
  119. #ifndef CONFIG_AMIGAONEG3SE
  120. #define ide_led(a,b) /* dummy */
  121. #else
  122. extern void ide_led(uchar led, uchar status);
  123. #define LED_IDE1 1
  124. #define LED_IDE2 2
  125. #define CONFIG_IDE_LED 1
  126. #define DEVICE_LED(x) 1
  127. #endif
  128. #endif
  129. #ifdef CONFIG_IDE_RESET
  130. static void ide_reset (void);
  131. #else
  132. #define ide_reset() /* dummy */
  133. #endif
  134. static void ide_ident (block_dev_desc_t *dev_desc);
  135. static uchar ide_wait (int dev, ulong t);
  136. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  137. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  138. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  139. void inline ide_outb(int dev, int port, unsigned char val);
  140. unsigned char inline ide_inb(int dev, int port);
  141. static void input_data(int dev, ulong *sect_buf, int words);
  142. static void output_data(int dev, ulong *sect_buf, int words);
  143. static void ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
  144. #ifndef CFG_ATA_PORT_ADDR
  145. #define CFG_ATA_PORT_ADDR(port) (port)
  146. #endif
  147. #ifdef CONFIG_ATAPI
  148. static void atapi_inquiry(block_dev_desc_t *dev_desc);
  149. ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer);
  150. #endif
  151. #ifdef CONFIG_IDE_8xx_DIRECT
  152. static void set_pcmcia_timing (int pmode);
  153. #endif
  154. /* ------------------------------------------------------------------------- */
  155. int do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  156. {
  157. int rcode = 0;
  158. switch (argc) {
  159. case 0:
  160. case 1:
  161. printf ("Usage:\n%s\n", cmdtp->usage);
  162. return 1;
  163. case 2:
  164. if (strncmp(argv[1],"res",3) == 0) {
  165. puts ("\nReset IDE"
  166. #ifdef CONFIG_IDE_8xx_DIRECT
  167. " on PCMCIA " PCMCIA_SLOT_MSG
  168. #endif
  169. ": ");
  170. ide_init ();
  171. return 0;
  172. } else if (strncmp(argv[1],"inf",3) == 0) {
  173. int i;
  174. putc ('\n');
  175. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  176. if (ide_dev_desc[i].type==DEV_TYPE_UNKNOWN)
  177. continue; /* list only known devices */
  178. printf ("IDE device %d: ", i);
  179. dev_print(&ide_dev_desc[i]);
  180. }
  181. return 0;
  182. } else if (strncmp(argv[1],"dev",3) == 0) {
  183. if ((curr_device < 0) || (curr_device >= CFG_IDE_MAXDEVICE)) {
  184. puts ("\nno IDE devices available\n");
  185. return 1;
  186. }
  187. printf ("\nIDE device %d: ", curr_device);
  188. dev_print(&ide_dev_desc[curr_device]);
  189. return 0;
  190. } else if (strncmp(argv[1],"part",4) == 0) {
  191. int dev, ok;
  192. for (ok=0, dev=0; dev<CFG_IDE_MAXDEVICE; ++dev) {
  193. if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
  194. ++ok;
  195. if (dev)
  196. putc ('\n');
  197. print_part(&ide_dev_desc[dev]);
  198. }
  199. }
  200. if (!ok) {
  201. puts ("\nno IDE devices available\n");
  202. rcode ++;
  203. }
  204. return rcode;
  205. }
  206. printf ("Usage:\n%s\n", cmdtp->usage);
  207. return 1;
  208. case 3:
  209. if (strncmp(argv[1],"dev",3) == 0) {
  210. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  211. printf ("\nIDE device %d: ", dev);
  212. if (dev >= CFG_IDE_MAXDEVICE) {
  213. puts ("unknown device\n");
  214. return 1;
  215. }
  216. dev_print(&ide_dev_desc[dev]);
  217. /*ide_print (dev);*/
  218. if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
  219. return 1;
  220. }
  221. curr_device = dev;
  222. puts ("... is now current device\n");
  223. return 0;
  224. } else if (strncmp(argv[1],"part",4) == 0) {
  225. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  226. if (ide_dev_desc[dev].part_type!=PART_TYPE_UNKNOWN) {
  227. print_part(&ide_dev_desc[dev]);
  228. } else {
  229. printf ("\nIDE device %d not available\n", dev);
  230. rcode = 1;
  231. }
  232. return rcode;
  233. #if 0
  234. } else if (strncmp(argv[1],"pio",4) == 0) {
  235. int mode = (int)simple_strtoul(argv[2], NULL, 10);
  236. if ((mode >= 0) && (mode <= IDE_MAX_PIO_MODE)) {
  237. puts ("\nSetting ");
  238. pio_mode = mode;
  239. ide_init ();
  240. } else {
  241. printf ("\nInvalid PIO mode %d (0 ... %d only)\n",
  242. mode, IDE_MAX_PIO_MODE);
  243. }
  244. return;
  245. #endif
  246. }
  247. printf ("Usage:\n%s\n", cmdtp->usage);
  248. return 1;
  249. default:
  250. /* at least 4 args */
  251. if (strcmp(argv[1],"read") == 0) {
  252. ulong addr = simple_strtoul(argv[2], NULL, 16);
  253. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  254. ulong n;
  255. #ifdef CFG_64BIT_STRTOUL
  256. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  257. printf ("\nIDE read: device %d block # %qd, count %ld ... ",
  258. curr_device, blk, cnt);
  259. #else
  260. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  261. printf ("\nIDE read: device %d block # %ld, count %ld ... ",
  262. curr_device, blk, cnt);
  263. #endif
  264. n = ide_dev_desc[curr_device].block_read (curr_device,
  265. blk, cnt,
  266. (ulong *)addr);
  267. /* flush cache after read */
  268. flush_cache (addr, cnt*ide_dev_desc[curr_device].blksz);
  269. printf ("%ld blocks read: %s\n",
  270. n, (n==cnt) ? "OK" : "ERROR");
  271. if (n==cnt) {
  272. return 0;
  273. } else {
  274. return 1;
  275. }
  276. } else if (strcmp(argv[1],"write") == 0) {
  277. ulong addr = simple_strtoul(argv[2], NULL, 16);
  278. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  279. ulong n;
  280. #ifdef CFG_64BIT_STRTOUL
  281. lbaint_t blk = simple_strtoull(argv[3], NULL, 16);
  282. printf ("\nIDE write: device %d block # %qd, count %ld ... ",
  283. curr_device, blk, cnt);
  284. #else
  285. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  286. printf ("\nIDE write: device %d block # %ld, count %ld ... ",
  287. curr_device, blk, cnt);
  288. #endif
  289. n = ide_write (curr_device, blk, cnt, (ulong *)addr);
  290. printf ("%ld blocks written: %s\n",
  291. n, (n==cnt) ? "OK" : "ERROR");
  292. if (n==cnt) {
  293. return 0;
  294. } else {
  295. return 1;
  296. }
  297. } else {
  298. printf ("Usage:\n%s\n", cmdtp->usage);
  299. rcode = 1;
  300. }
  301. return rcode;
  302. }
  303. }
  304. int do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  305. {
  306. char *boot_device = NULL;
  307. char *ep;
  308. int dev, part = 0;
  309. ulong addr, cnt;
  310. disk_partition_t info;
  311. image_header_t *hdr;
  312. int rcode = 0;
  313. #if defined(CONFIG_FIT)
  314. const void *fit_hdr;
  315. #endif
  316. show_boot_progress (41);
  317. switch (argc) {
  318. case 1:
  319. addr = CFG_LOAD_ADDR;
  320. boot_device = getenv ("bootdevice");
  321. break;
  322. case 2:
  323. addr = simple_strtoul(argv[1], NULL, 16);
  324. boot_device = getenv ("bootdevice");
  325. break;
  326. case 3:
  327. addr = simple_strtoul(argv[1], NULL, 16);
  328. boot_device = argv[2];
  329. break;
  330. default:
  331. printf ("Usage:\n%s\n", cmdtp->usage);
  332. show_boot_progress (-42);
  333. return 1;
  334. }
  335. show_boot_progress (42);
  336. if (!boot_device) {
  337. puts ("\n** No boot device **\n");
  338. show_boot_progress (-43);
  339. return 1;
  340. }
  341. show_boot_progress (43);
  342. dev = simple_strtoul(boot_device, &ep, 16);
  343. if (ide_dev_desc[dev].type==DEV_TYPE_UNKNOWN) {
  344. printf ("\n** Device %d not available\n", dev);
  345. show_boot_progress (-44);
  346. return 1;
  347. }
  348. show_boot_progress (44);
  349. if (*ep) {
  350. if (*ep != ':') {
  351. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  352. show_boot_progress (-45);
  353. return 1;
  354. }
  355. part = simple_strtoul(++ep, NULL, 16);
  356. }
  357. show_boot_progress (45);
  358. if (get_partition_info (&ide_dev_desc[dev], part, &info)) {
  359. show_boot_progress (-46);
  360. return 1;
  361. }
  362. show_boot_progress (46);
  363. if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
  364. (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
  365. printf ("\n** Invalid partition type \"%.32s\""
  366. " (expect \"" BOOT_PART_TYPE "\")\n",
  367. info.type);
  368. show_boot_progress (-47);
  369. return 1;
  370. }
  371. show_boot_progress (47);
  372. printf ("\nLoading from IDE device %d, partition %d: "
  373. "Name: %.32s Type: %.32s\n",
  374. dev, part, info.name, info.type);
  375. debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
  376. info.start, info.size, info.blksz);
  377. if (ide_dev_desc[dev].block_read (dev, info.start, 1, (ulong *)addr) != 1) {
  378. printf ("** Read error on %d:%d\n", dev, part);
  379. show_boot_progress (-48);
  380. return 1;
  381. }
  382. show_boot_progress (48);
  383. switch (genimg_get_format ((void *)addr)) {
  384. case IMAGE_FORMAT_LEGACY:
  385. hdr = (image_header_t *)addr;
  386. show_boot_progress (49);
  387. if (!image_check_hcrc (hdr)) {
  388. puts ("\n** Bad Header Checksum **\n");
  389. show_boot_progress (-50);
  390. return 1;
  391. }
  392. show_boot_progress (50);
  393. image_print_contents (hdr);
  394. cnt = image_get_image_size (hdr);
  395. break;
  396. #if defined(CONFIG_FIT)
  397. case IMAGE_FORMAT_FIT:
  398. fit_hdr = (const void *)addr;
  399. if (!fit_check_format (fit_hdr)) {
  400. show_boot_progress (-140);
  401. puts ("** Bad FIT image format\n");
  402. return 1;
  403. }
  404. show_boot_progress (141);
  405. puts ("Fit image detected...\n");
  406. cnt = fit_get_size (fit_hdr);
  407. break;
  408. #endif
  409. default:
  410. show_boot_progress (-49);
  411. puts ("** Unknown image type\n");
  412. return 1;
  413. }
  414. cnt += info.blksz - 1;
  415. cnt /= info.blksz;
  416. cnt -= 1;
  417. if (ide_dev_desc[dev].block_read (dev, info.start+1, cnt,
  418. (ulong *)(addr+info.blksz)) != cnt) {
  419. printf ("** Read error on %d:%d\n", dev, part);
  420. show_boot_progress (-51);
  421. return 1;
  422. }
  423. show_boot_progress (51);
  424. #if defined(CONFIG_FIT)
  425. /* This cannot be done earlier, we need complete FIT image in RAM first */
  426. if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
  427. fit_print_contents ((const void *)addr);
  428. #endif
  429. /* Loading ok, update default load address */
  430. load_addr = addr;
  431. /* Check if we should attempt an auto-start */
  432. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  433. char *local_args[2];
  434. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  435. local_args[0] = argv[0];
  436. local_args[1] = NULL;
  437. printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
  438. do_bootm (cmdtp, 0, 1, local_args);
  439. rcode = 1;
  440. }
  441. return rcode;
  442. }
  443. /* ------------------------------------------------------------------------- */
  444. void ide_init (void)
  445. {
  446. #ifdef CONFIG_IDE_8xx_DIRECT
  447. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  448. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  449. #endif
  450. unsigned char c;
  451. int i, bus;
  452. #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
  453. unsigned int ata_reset_time = ATA_RESET_TIME;
  454. char *s;
  455. #endif
  456. #ifdef CONFIG_AMIGAONEG3SE
  457. unsigned int max_bus_scan;
  458. #endif
  459. #ifdef CONFIG_IDE_8xx_PCCARD
  460. extern int pcmcia_on (void);
  461. extern int ide_devices_found; /* Initialized in check_ide_device() */
  462. #endif /* CONFIG_IDE_8xx_PCCARD */
  463. #ifdef CONFIG_IDE_PREINIT
  464. extern int ide_preinit (void);
  465. WATCHDOG_RESET();
  466. if (ide_preinit ()) {
  467. puts ("ide_preinit failed\n");
  468. return;
  469. }
  470. #endif /* CONFIG_IDE_PREINIT */
  471. #ifdef CONFIG_IDE_8xx_PCCARD
  472. extern int pcmcia_on (void);
  473. extern int ide_devices_found; /* Initialized in check_ide_device() */
  474. WATCHDOG_RESET();
  475. ide_devices_found = 0;
  476. /* initialize the PCMCIA IDE adapter card */
  477. pcmcia_on();
  478. if (!ide_devices_found)
  479. return;
  480. udelay (1000000); /* 1 s */
  481. #endif /* CONFIG_IDE_8xx_PCCARD */
  482. WATCHDOG_RESET();
  483. #ifdef CONFIG_IDE_8xx_DIRECT
  484. /* Initialize PIO timing tables */
  485. for (i=0; i <= IDE_MAX_PIO_MODE; ++i) {
  486. pio_config_clk[i].t_setup = PCMCIA_MK_CLKS(pio_config_ns[i].t_setup,
  487. gd->bus_clk);
  488. pio_config_clk[i].t_length = PCMCIA_MK_CLKS(pio_config_ns[i].t_length,
  489. gd->bus_clk);
  490. pio_config_clk[i].t_hold = PCMCIA_MK_CLKS(pio_config_ns[i].t_hold,
  491. gd->bus_clk);
  492. debug ( "PIO Mode %d: setup=%2d ns/%d clk"
  493. " len=%3d ns/%d clk"
  494. " hold=%2d ns/%d clk\n",
  495. i,
  496. pio_config_ns[i].t_setup, pio_config_clk[i].t_setup,
  497. pio_config_ns[i].t_length, pio_config_clk[i].t_length,
  498. pio_config_ns[i].t_hold, pio_config_clk[i].t_hold);
  499. }
  500. #endif /* CONFIG_IDE_8xx_DIRECT */
  501. /* Reset the IDE just to be sure.
  502. * Light LED's to show
  503. */
  504. ide_led ((LED_IDE1 | LED_IDE2), 1); /* LED's on */
  505. ide_reset (); /* ATAPI Drives seems to need a proper IDE Reset */
  506. #ifdef CONFIG_IDE_8xx_DIRECT
  507. /* PCMCIA / IDE initialization for common mem space */
  508. pcmp->pcmc_pgcrb = 0;
  509. /* start in PIO mode 0 - most relaxed timings */
  510. pio_mode = 0;
  511. set_pcmcia_timing (pio_mode);
  512. #endif /* CONFIG_IDE_8xx_DIRECT */
  513. /*
  514. * Wait for IDE to get ready.
  515. * According to spec, this can take up to 31 seconds!
  516. */
  517. #ifndef CONFIG_AMIGAONEG3SE
  518. for (bus=0; bus<CFG_IDE_MAXBUS; ++bus) {
  519. int dev = bus * (CFG_IDE_MAXDEVICE / CFG_IDE_MAXBUS);
  520. #else
  521. s = getenv("ide_maxbus");
  522. if (s)
  523. max_bus_scan = simple_strtol(s, NULL, 10);
  524. else
  525. max_bus_scan = CFG_IDE_MAXBUS;
  526. for (bus=0; bus<max_bus_scan; ++bus) {
  527. int dev = bus * (CFG_IDE_MAXDEVICE / max_bus_scan);
  528. #endif
  529. #ifdef CONFIG_IDE_8xx_PCCARD
  530. /* Skip non-ide devices from probing */
  531. if ((ide_devices_found & (1 << bus)) == 0) {
  532. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  533. continue;
  534. }
  535. #endif
  536. printf ("Bus %d: ", bus);
  537. ide_bus_ok[bus] = 0;
  538. /* Select device
  539. */
  540. udelay (100000); /* 100 ms */
  541. ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  542. udelay (100000); /* 100 ms */
  543. #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
  544. if ((s = getenv("ide_reset_timeout")) != NULL)
  545. ata_reset_time = simple_strtol(s, NULL, 10);
  546. #endif
  547. i = 0;
  548. do {
  549. udelay (10000); /* 10 ms */
  550. c = ide_inb (dev, ATA_STATUS);
  551. i++;
  552. #if defined(CONFIG_AMIGAONEG3SE) || defined(CONFIG_SC3)
  553. if (i > (ata_reset_time * 100)) {
  554. #else
  555. if (i > (ATA_RESET_TIME * 100)) {
  556. #endif
  557. puts ("** Timeout **\n");
  558. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  559. #ifdef CONFIG_AMIGAONEG3SE
  560. /* If this is the second bus, the first one was OK */
  561. if (bus != 0) {
  562. ide_bus_ok[bus] = 0;
  563. goto skip_bus;
  564. }
  565. #endif
  566. return;
  567. }
  568. if ((i >= 100) && ((i%100)==0)) {
  569. putc ('.');
  570. }
  571. } while (c & ATA_STAT_BUSY);
  572. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  573. puts ("not available ");
  574. debug ("Status = 0x%02X ", c);
  575. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  576. } else if ((c & ATA_STAT_READY) == 0) {
  577. puts ("not available ");
  578. debug ("Status = 0x%02X ", c);
  579. #endif
  580. } else {
  581. puts ("OK ");
  582. ide_bus_ok[bus] = 1;
  583. }
  584. WATCHDOG_RESET();
  585. }
  586. #ifdef CONFIG_AMIGAONEG3SE
  587. skip_bus:
  588. #endif
  589. putc ('\n');
  590. ide_led ((LED_IDE1 | LED_IDE2), 0); /* LED's off */
  591. curr_device = -1;
  592. for (i=0; i<CFG_IDE_MAXDEVICE; ++i) {
  593. #ifdef CONFIG_IDE_LED
  594. int led = (IDE_BUS(i) == 0) ? LED_IDE1 : LED_IDE2;
  595. #endif
  596. ide_dev_desc[i].type=DEV_TYPE_UNKNOWN;
  597. ide_dev_desc[i].if_type=IF_TYPE_IDE;
  598. ide_dev_desc[i].dev=i;
  599. ide_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
  600. ide_dev_desc[i].blksz=0;
  601. ide_dev_desc[i].lba=0;
  602. ide_dev_desc[i].block_read=ide_read;
  603. if (!ide_bus_ok[IDE_BUS(i)])
  604. continue;
  605. ide_led (led, 1); /* LED on */
  606. ide_ident(&ide_dev_desc[i]);
  607. ide_led (led, 0); /* LED off */
  608. dev_print(&ide_dev_desc[i]);
  609. /* ide_print (i); */
  610. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  611. init_part (&ide_dev_desc[i]); /* initialize partition type */
  612. if (curr_device < 0)
  613. curr_device = i;
  614. }
  615. }
  616. WATCHDOG_RESET();
  617. }
  618. /* ------------------------------------------------------------------------- */
  619. block_dev_desc_t * ide_get_dev(int dev)
  620. {
  621. return (dev < CFG_IDE_MAXDEVICE) ? &ide_dev_desc[dev] : NULL;
  622. }
  623. #ifdef CONFIG_IDE_8xx_DIRECT
  624. static void
  625. set_pcmcia_timing (int pmode)
  626. {
  627. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  628. volatile pcmconf8xx_t *pcmp = &(immr->im_pcmcia);
  629. ulong timings;
  630. debug ("Set timing for PIO Mode %d\n", pmode);
  631. timings = PCMCIA_SHT(pio_config_clk[pmode].t_hold)
  632. | PCMCIA_SST(pio_config_clk[pmode].t_setup)
  633. | PCMCIA_SL (pio_config_clk[pmode].t_length)
  634. ;
  635. /* IDE 0
  636. */
  637. pcmp->pcmc_pbr0 = CFG_PCMCIA_PBR0;
  638. pcmp->pcmc_por0 = CFG_PCMCIA_POR0
  639. #if (CFG_PCMCIA_POR0 != 0)
  640. | timings
  641. #endif
  642. ;
  643. debug ("PBR0: %08x POR0: %08x\n", pcmp->pcmc_pbr0, pcmp->pcmc_por0);
  644. pcmp->pcmc_pbr1 = CFG_PCMCIA_PBR1;
  645. pcmp->pcmc_por1 = CFG_PCMCIA_POR1
  646. #if (CFG_PCMCIA_POR1 != 0)
  647. | timings
  648. #endif
  649. ;
  650. debug ("PBR1: %08x POR1: %08x\n", pcmp->pcmc_pbr1, pcmp->pcmc_por1);
  651. pcmp->pcmc_pbr2 = CFG_PCMCIA_PBR2;
  652. pcmp->pcmc_por2 = CFG_PCMCIA_POR2
  653. #if (CFG_PCMCIA_POR2 != 0)
  654. | timings
  655. #endif
  656. ;
  657. debug ("PBR2: %08x POR2: %08x\n", pcmp->pcmc_pbr2, pcmp->pcmc_por2);
  658. pcmp->pcmc_pbr3 = CFG_PCMCIA_PBR3;
  659. pcmp->pcmc_por3 = CFG_PCMCIA_POR3
  660. #if (CFG_PCMCIA_POR3 != 0)
  661. | timings
  662. #endif
  663. ;
  664. debug ("PBR3: %08x POR3: %08x\n", pcmp->pcmc_pbr3, pcmp->pcmc_por3);
  665. /* IDE 1
  666. */
  667. pcmp->pcmc_pbr4 = CFG_PCMCIA_PBR4;
  668. pcmp->pcmc_por4 = CFG_PCMCIA_POR4
  669. #if (CFG_PCMCIA_POR4 != 0)
  670. | timings
  671. #endif
  672. ;
  673. debug ("PBR4: %08x POR4: %08x\n", pcmp->pcmc_pbr4, pcmp->pcmc_por4);
  674. pcmp->pcmc_pbr5 = CFG_PCMCIA_PBR5;
  675. pcmp->pcmc_por5 = CFG_PCMCIA_POR5
  676. #if (CFG_PCMCIA_POR5 != 0)
  677. | timings
  678. #endif
  679. ;
  680. debug ("PBR5: %08x POR5: %08x\n", pcmp->pcmc_pbr5, pcmp->pcmc_por5);
  681. pcmp->pcmc_pbr6 = CFG_PCMCIA_PBR6;
  682. pcmp->pcmc_por6 = CFG_PCMCIA_POR6
  683. #if (CFG_PCMCIA_POR6 != 0)
  684. | timings
  685. #endif
  686. ;
  687. debug ("PBR6: %08x POR6: %08x\n", pcmp->pcmc_pbr6, pcmp->pcmc_por6);
  688. pcmp->pcmc_pbr7 = CFG_PCMCIA_PBR7;
  689. pcmp->pcmc_por7 = CFG_PCMCIA_POR7
  690. #if (CFG_PCMCIA_POR7 != 0)
  691. | timings
  692. #endif
  693. ;
  694. debug ("PBR7: %08x POR7: %08x\n", pcmp->pcmc_pbr7, pcmp->pcmc_por7);
  695. }
  696. #endif /* CONFIG_IDE_8xx_DIRECT */
  697. /* ------------------------------------------------------------------------- */
  698. void inline
  699. __ide_outb(int dev, int port, unsigned char val)
  700. {
  701. debug ("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
  702. dev, port, val, (ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port)));
  703. outb(val, (ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port)));
  704. }
  705. void inline ide_outb (int dev, int port, unsigned char val)
  706. __attribute__((weak, alias("__ide_outb")));
  707. unsigned char inline
  708. __ide_inb(int dev, int port)
  709. {
  710. uchar val;
  711. val = inb((ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port)));
  712. debug ("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
  713. dev, port, (ATA_CURR_BASE(dev)+CFG_ATA_PORT_ADDR(port)), val);
  714. return val;
  715. }
  716. unsigned char inline ide_inb(int dev, int port)
  717. __attribute__((weak, alias("__ide_inb")));
  718. #ifdef __PPC__
  719. # ifdef CONFIG_AMIGAONEG3SE
  720. static void
  721. output_data_short(int dev, ulong *sect_buf, int words)
  722. {
  723. ushort *dbuf;
  724. volatile ushort *pbuf;
  725. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  726. dbuf = (ushort *)sect_buf;
  727. while (words--) {
  728. EIEIO;
  729. *pbuf = *dbuf++;
  730. EIEIO;
  731. }
  732. if (words&1)
  733. *pbuf = 0;
  734. }
  735. # endif /* CONFIG_AMIGAONEG3SE */
  736. #endif /* __PPC_ */
  737. /* We only need to swap data if we are running on a big endian cpu. */
  738. /* But Au1x00 cpu:s already swaps data in big endian mode! */
  739. #if defined(__LITTLE_ENDIAN) || ( defined(CONFIG_AU1X00) && !defined(CONFIG_GTH2) )
  740. #define input_swap_data(x,y,z) input_data(x,y,z)
  741. #else
  742. static void
  743. input_swap_data(int dev, ulong *sect_buf, int words)
  744. {
  745. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  746. uchar i;
  747. volatile uchar *pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  748. volatile uchar *pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  749. ushort *dbuf = (ushort *)sect_buf;
  750. while (words--) {
  751. for (i=0; i<2; i++) {
  752. *(((uchar *)(dbuf)) + 1) = *pbuf_even;
  753. *(uchar *)dbuf = *pbuf_odd;
  754. dbuf+=1;
  755. }
  756. }
  757. #else
  758. volatile ushort *pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  759. ushort *dbuf = (ushort *)sect_buf;
  760. debug("in input swap data base for read is %lx\n", (unsigned long) pbuf);
  761. while (words--) {
  762. #ifdef __MIPS__
  763. *dbuf++ = swab16p((u16*)pbuf);
  764. *dbuf++ = swab16p((u16*)pbuf);
  765. #elif defined(CONFIG_PCS440EP)
  766. *dbuf++ = *pbuf;
  767. *dbuf++ = *pbuf;
  768. #else
  769. *dbuf++ = ld_le16(pbuf);
  770. *dbuf++ = ld_le16(pbuf);
  771. #endif /* !MIPS */
  772. }
  773. #endif
  774. }
  775. #endif /* __LITTLE_ENDIAN || CONFIG_AU1X00 */
  776. #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA) || defined(CONFIG_SH)
  777. static void
  778. output_data(int dev, ulong *sect_buf, int words)
  779. {
  780. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  781. uchar *dbuf;
  782. volatile uchar *pbuf_even;
  783. volatile uchar *pbuf_odd;
  784. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  785. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  786. dbuf = (uchar *)sect_buf;
  787. while (words--) {
  788. EIEIO;
  789. *pbuf_even = *dbuf++;
  790. EIEIO;
  791. *pbuf_odd = *dbuf++;
  792. EIEIO;
  793. *pbuf_even = *dbuf++;
  794. EIEIO;
  795. *pbuf_odd = *dbuf++;
  796. }
  797. #else
  798. ushort *dbuf;
  799. volatile ushort *pbuf;
  800. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  801. dbuf = (ushort *)sect_buf;
  802. while (words--) {
  803. #if defined(CONFIG_PCS440EP)
  804. /* not tested, because CF was write protected */
  805. EIEIO;
  806. *pbuf = ld_le16(dbuf++);
  807. EIEIO;
  808. *pbuf = ld_le16(dbuf++);
  809. #else
  810. EIEIO;
  811. *pbuf = *dbuf++;
  812. EIEIO;
  813. *pbuf = *dbuf++;
  814. #endif
  815. }
  816. #endif
  817. }
  818. #else /* ! __PPC__ */
  819. static void
  820. output_data(int dev, ulong *sect_buf, int words)
  821. {
  822. outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words<<1);
  823. }
  824. #endif /* __PPC__ */
  825. #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA) || defined(CONFIG_SH)
  826. static void
  827. input_data(int dev, ulong *sect_buf, int words)
  828. {
  829. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  830. uchar *dbuf;
  831. volatile uchar *pbuf_even;
  832. volatile uchar *pbuf_odd;
  833. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  834. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  835. dbuf = (uchar *)sect_buf;
  836. while (words--) {
  837. *dbuf++ = *pbuf_even;
  838. EIEIO;
  839. SYNC;
  840. *dbuf++ = *pbuf_odd;
  841. EIEIO;
  842. SYNC;
  843. *dbuf++ = *pbuf_even;
  844. EIEIO;
  845. SYNC;
  846. *dbuf++ = *pbuf_odd;
  847. EIEIO;
  848. SYNC;
  849. }
  850. #else
  851. ushort *dbuf;
  852. volatile ushort *pbuf;
  853. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  854. dbuf = (ushort *)sect_buf;
  855. debug("in input data base for read is %lx\n", (unsigned long) pbuf);
  856. while (words--) {
  857. #if defined(CONFIG_PCS440EP)
  858. EIEIO;
  859. *dbuf++ = ld_le16(pbuf);
  860. EIEIO;
  861. *dbuf++ = ld_le16(pbuf);
  862. #else
  863. EIEIO;
  864. *dbuf++ = *pbuf;
  865. EIEIO;
  866. *dbuf++ = *pbuf;
  867. #endif
  868. }
  869. #endif
  870. }
  871. #else /* ! __PPC__ */
  872. static void
  873. input_data(int dev, ulong *sect_buf, int words)
  874. {
  875. insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, words << 1);
  876. }
  877. #endif /* __PPC__ */
  878. #ifdef CONFIG_AMIGAONEG3SE
  879. static void
  880. input_data_short(int dev, ulong *sect_buf, int words)
  881. {
  882. ushort *dbuf;
  883. volatile ushort *pbuf;
  884. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  885. dbuf = (ushort *)sect_buf;
  886. while (words--) {
  887. EIEIO;
  888. *dbuf++ = *pbuf;
  889. EIEIO;
  890. }
  891. if (words&1) {
  892. ushort dummy;
  893. dummy = *pbuf;
  894. }
  895. }
  896. #endif
  897. /* -------------------------------------------------------------------------
  898. */
  899. static void ide_ident (block_dev_desc_t *dev_desc)
  900. {
  901. ulong iobuf[ATA_SECTORWORDS];
  902. unsigned char c;
  903. hd_driveid_t *iop = (hd_driveid_t *)iobuf;
  904. #ifdef CONFIG_AMIGAONEG3SE
  905. int max_bus_scan;
  906. char *s;
  907. #endif
  908. #ifdef CONFIG_ATAPI
  909. int retries = 0;
  910. int do_retry = 0;
  911. #endif
  912. #if 0
  913. int mode, cycle_time;
  914. #endif
  915. int device;
  916. device=dev_desc->dev;
  917. printf (" Device %d: ", device);
  918. #ifdef CONFIG_AMIGAONEG3SE
  919. s = getenv("ide_maxbus");
  920. if (s) {
  921. max_bus_scan = simple_strtol(s, NULL, 10);
  922. } else {
  923. max_bus_scan = CFG_IDE_MAXBUS;
  924. }
  925. if (device >= max_bus_scan*2) {
  926. dev_desc->type=DEV_TYPE_UNKNOWN;
  927. return;
  928. }
  929. #endif
  930. ide_led (DEVICE_LED(device), 1); /* LED on */
  931. /* Select device
  932. */
  933. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  934. dev_desc->if_type=IF_TYPE_IDE;
  935. #ifdef CONFIG_ATAPI
  936. do_retry = 0;
  937. retries = 0;
  938. /* Warning: This will be tricky to read */
  939. while (retries <= 1) {
  940. /* check signature */
  941. if ((ide_inb(device,ATA_SECT_CNT) == 0x01) &&
  942. (ide_inb(device,ATA_SECT_NUM) == 0x01) &&
  943. (ide_inb(device,ATA_CYL_LOW) == 0x14) &&
  944. (ide_inb(device,ATA_CYL_HIGH) == 0xEB)) {
  945. /* ATAPI Signature found */
  946. dev_desc->if_type=IF_TYPE_ATAPI;
  947. /* Start Ident Command
  948. */
  949. ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT);
  950. /*
  951. * Wait for completion - ATAPI devices need more time
  952. * to become ready
  953. */
  954. c = ide_wait (device, ATAPI_TIME_OUT);
  955. } else
  956. #endif
  957. {
  958. /* Start Ident Command
  959. */
  960. ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT);
  961. /* Wait for completion
  962. */
  963. c = ide_wait (device, IDE_TIME_OUT);
  964. }
  965. ide_led (DEVICE_LED(device), 0); /* LED off */
  966. if (((c & ATA_STAT_DRQ) == 0) ||
  967. ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) ) {
  968. #ifdef CONFIG_ATAPI
  969. #ifdef CONFIG_AMIGAONEG3SE
  970. s = getenv("ide_doreset");
  971. if (s && strcmp(s, "on") == 0)
  972. #endif
  973. {
  974. /* Need to soft reset the device in case it's an ATAPI... */
  975. debug ("Retrying...\n");
  976. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  977. udelay(100000);
  978. ide_outb (device, ATA_COMMAND, 0x08);
  979. udelay (500000); /* 500 ms */
  980. }
  981. /* Select device
  982. */
  983. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  984. retries++;
  985. #else
  986. return;
  987. #endif
  988. }
  989. #ifdef CONFIG_ATAPI
  990. else
  991. break;
  992. } /* see above - ugly to read */
  993. if (retries == 2) /* Not found */
  994. return;
  995. #endif
  996. input_swap_data (device, iobuf, ATA_SECTORWORDS);
  997. ident_cpy ((unsigned char*)dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
  998. ident_cpy ((unsigned char*)dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
  999. ident_cpy ((unsigned char*)dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
  1000. #ifdef __LITTLE_ENDIAN
  1001. /*
  1002. * firmware revision and model number have Big Endian Byte
  1003. * order in Word. Convert both to little endian.
  1004. *
  1005. * See CF+ and CompactFlash Specification Revision 2.0:
  1006. * 6.2.1.6: Identfy Drive, Table 39 for more details
  1007. */
  1008. strswab (dev_desc->revision);
  1009. strswab (dev_desc->vendor);
  1010. #endif /* __LITTLE_ENDIAN */
  1011. if ((iop->config & 0x0080)==0x0080)
  1012. dev_desc->removable = 1;
  1013. else
  1014. dev_desc->removable = 0;
  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 %qX, 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 CFG_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(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
  1175. printf ("Error (no IRQ) dev %d blk %qd: 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 CFG_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(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
  1249. printf ("Error (no IRQ) dev %d blk %qd: 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(CFG_PB_12V_ENABLE) || defined(CFG_PB_IDE_MOTOR)
  1316. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  1317. #endif
  1318. int i;
  1319. curr_device = -1;
  1320. for (i=0; i<CFG_IDE_MAXBUS; ++i)
  1321. ide_bus_ok[i] = 0;
  1322. for (i=0; i<CFG_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 CFG_PB_12V_ENABLE
  1329. immr->im_cpm.cp_pbdat &= ~(CFG_PB_12V_ENABLE); /* 12V Enable output OFF */
  1330. immr->im_cpm.cp_pbpar &= ~(CFG_PB_12V_ENABLE);
  1331. immr->im_cpm.cp_pbodr &= ~(CFG_PB_12V_ENABLE);
  1332. immr->im_cpm.cp_pbdir |= CFG_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 |= CFG_PB_12V_ENABLE; /* 12V Enable output ON */
  1339. #endif /* CFG_PB_12V_ENABLE */
  1340. #ifdef CFG_PB_IDE_MOTOR
  1341. /* configure IDE Motor voltage monitor pin as input */
  1342. immr->im_cpm.cp_pbpar &= ~(CFG_PB_IDE_MOTOR);
  1343. immr->im_cpm.cp_pbodr &= ~(CFG_PB_IDE_MOTOR);
  1344. immr->im_cpm.cp_pbdir &= ~(CFG_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 & CFG_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 /* CFG_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. /* ------------------------------------------------------------------------- */
  1394. #ifdef CONFIG_ATAPI
  1395. /****************************************************************************
  1396. * ATAPI Support
  1397. */
  1398. #if defined(__PPC__) || defined(CONFIG_PXA_PCMCIA)
  1399. /* since ATAPI may use commands with not 4 bytes alligned length
  1400. * we have our own transfer functions, 2 bytes alligned */
  1401. static void
  1402. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1403. {
  1404. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  1405. uchar *dbuf;
  1406. volatile uchar *pbuf_even;
  1407. volatile uchar *pbuf_odd;
  1408. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  1409. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  1410. while (shorts--) {
  1411. EIEIO;
  1412. *pbuf_even = *dbuf++;
  1413. EIEIO;
  1414. *pbuf_odd = *dbuf++;
  1415. }
  1416. #else
  1417. ushort *dbuf;
  1418. volatile ushort *pbuf;
  1419. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1420. dbuf = (ushort *)sect_buf;
  1421. debug ("in output data shorts base for read is %lx\n", (unsigned long) pbuf);
  1422. while (shorts--) {
  1423. EIEIO;
  1424. *pbuf = *dbuf++;
  1425. }
  1426. #endif
  1427. }
  1428. static void
  1429. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1430. {
  1431. #if defined(CONFIG_HMI10) || defined(CONFIG_CPC45)
  1432. uchar *dbuf;
  1433. volatile uchar *pbuf_even;
  1434. volatile uchar *pbuf_odd;
  1435. pbuf_even = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_EVEN);
  1436. pbuf_odd = (uchar *)(ATA_CURR_BASE(dev)+ATA_DATA_ODD);
  1437. while (shorts--) {
  1438. EIEIO;
  1439. *dbuf++ = *pbuf_even;
  1440. EIEIO;
  1441. *dbuf++ = *pbuf_odd;
  1442. }
  1443. #else
  1444. ushort *dbuf;
  1445. volatile ushort *pbuf;
  1446. pbuf = (ushort *)(ATA_CURR_BASE(dev)+ATA_DATA_REG);
  1447. dbuf = (ushort *)sect_buf;
  1448. debug("in input data shorts base for read is %lx\n", (unsigned long) pbuf);
  1449. while (shorts--) {
  1450. EIEIO;
  1451. *dbuf++ = *pbuf;
  1452. }
  1453. #endif
  1454. }
  1455. #else /* ! __PPC__ */
  1456. static void
  1457. output_data_shorts(int dev, ushort *sect_buf, int shorts)
  1458. {
  1459. outsw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
  1460. }
  1461. static void
  1462. input_data_shorts(int dev, ushort *sect_buf, int shorts)
  1463. {
  1464. insw(ATA_CURR_BASE(dev)+ATA_DATA_REG, sect_buf, shorts);
  1465. }
  1466. #endif /* __PPC__ */
  1467. /*
  1468. * Wait until (Status & mask) == res, or timeout (in ms)
  1469. * Return last status
  1470. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  1471. * and then they set their DRQ Bit
  1472. */
  1473. static uchar atapi_wait_mask (int dev, ulong t,uchar mask, uchar res)
  1474. {
  1475. ulong delay = 10 * t; /* poll every 100 us */
  1476. uchar c;
  1477. c = ide_inb(dev,ATA_DEV_CTL); /* prevents to read the status before valid */
  1478. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  1479. /* break if error occurs (doesn't make sense to wait more) */
  1480. if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
  1481. break;
  1482. udelay (100);
  1483. if (delay-- == 0) {
  1484. break;
  1485. }
  1486. }
  1487. return (c);
  1488. }
  1489. /*
  1490. * issue an atapi command
  1491. */
  1492. unsigned char atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen)
  1493. {
  1494. unsigned char c,err,mask,res;
  1495. int n;
  1496. ide_led (DEVICE_LED(device), 1); /* LED on */
  1497. /* Select device
  1498. */
  1499. mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
  1500. res = 0;
  1501. #ifdef CONFIG_AMIGAONEG3SE
  1502. # warning THF: Removed LBA mode ???
  1503. #endif
  1504. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1505. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1506. if ((c & mask) != res) {
  1507. printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
  1508. err=0xFF;
  1509. goto AI_OUT;
  1510. }
  1511. /* write taskfile */
  1512. ide_outb (device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  1513. ide_outb (device, ATA_SECT_CNT, 0);
  1514. ide_outb (device, ATA_SECT_NUM, 0);
  1515. ide_outb (device, ATA_CYL_LOW, (unsigned char)(buflen & 0xFF));
  1516. ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF));
  1517. #ifdef CONFIG_AMIGAONEG3SE
  1518. # warning THF: Removed LBA mode ???
  1519. #endif
  1520. ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  1521. ide_outb (device, ATA_COMMAND, ATAPI_CMD_PACKET);
  1522. udelay (50);
  1523. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1524. res = ATA_STAT_DRQ;
  1525. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1526. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  1527. printf ("ATTAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
  1528. err=0xFF;
  1529. goto AI_OUT;
  1530. }
  1531. output_data_shorts (device, (unsigned short *)ccb,ccblen/2); /* write command block */
  1532. /* ATAPI Command written wait for completition */
  1533. udelay (5000); /* device must set bsy */
  1534. mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
  1535. /* if no data wait for DRQ = 0 BSY = 0
  1536. * if data wait for DRQ = 1 BSY = 0 */
  1537. res=0;
  1538. if(buflen)
  1539. res = ATA_STAT_DRQ;
  1540. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1541. if ((c & mask) != res ) {
  1542. if (c & ATA_STAT_ERR) {
  1543. err=(ide_inb(device,ATA_ERROR_REG))>>4;
  1544. debug ("atapi_issue 1 returned sense key %X status %02X\n",err,c);
  1545. } else {
  1546. printf ("ATTAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
  1547. err=0xFF;
  1548. }
  1549. goto AI_OUT;
  1550. }
  1551. n=ide_inb(device, ATA_CYL_HIGH);
  1552. n<<=8;
  1553. n+=ide_inb(device, ATA_CYL_LOW);
  1554. if(n>buflen) {
  1555. printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
  1556. err=0xff;
  1557. goto AI_OUT;
  1558. }
  1559. if((n==0)&&(buflen<0)) {
  1560. printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
  1561. err=0xff;
  1562. goto AI_OUT;
  1563. }
  1564. if(n!=buflen) {
  1565. debug ("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
  1566. }
  1567. if(n!=0) { /* data transfer */
  1568. debug ("ATAPI_ISSUE: %d Bytes to transfer\n",n);
  1569. /* we transfer shorts */
  1570. n>>=1;
  1571. /* ok now decide if it is an in or output */
  1572. if ((ide_inb(device, ATA_SECT_CNT)&0x02)==0) {
  1573. debug ("Write to device\n");
  1574. output_data_shorts(device,(unsigned short *)buffer,n);
  1575. } else {
  1576. debug ("Read from device @ %p shorts %d\n",buffer,n);
  1577. input_data_shorts(device,(unsigned short *)buffer,n);
  1578. }
  1579. }
  1580. udelay(5000); /* seems that some CD ROMs need this... */
  1581. mask = ATA_STAT_BUSY|ATA_STAT_ERR;
  1582. res=0;
  1583. c = atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res);
  1584. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  1585. err=(ide_inb(device,ATA_ERROR_REG) >> 4);
  1586. debug ("atapi_issue 2 returned sense key %X status %X\n",err,c);
  1587. } else {
  1588. err = 0;
  1589. }
  1590. AI_OUT:
  1591. ide_led (DEVICE_LED(device), 0); /* LED off */
  1592. return (err);
  1593. }
  1594. /*
  1595. * sending the command to atapi_issue. If an status other than good
  1596. * returns, an request_sense will be issued
  1597. */
  1598. #define ATAPI_DRIVE_NOT_READY 100
  1599. #define ATAPI_UNIT_ATTN 10
  1600. unsigned char atapi_issue_autoreq (int device,
  1601. unsigned char* ccb,
  1602. int ccblen,
  1603. unsigned char *buffer,
  1604. int buflen)
  1605. {
  1606. unsigned char sense_data[18],sense_ccb[12];
  1607. unsigned char res,key,asc,ascq;
  1608. int notready,unitattn;
  1609. #ifdef CONFIG_AMIGAONEG3SE
  1610. char *s;
  1611. unsigned int timeout, retrycnt;
  1612. s = getenv("ide_cd_timeout");
  1613. timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
  1614. retrycnt = 0;
  1615. #endif
  1616. unitattn=ATAPI_UNIT_ATTN;
  1617. notready=ATAPI_DRIVE_NOT_READY;
  1618. retry:
  1619. res= atapi_issue(device,ccb,ccblen,buffer,buflen);
  1620. if (res==0)
  1621. return (0); /* Ok */
  1622. if (res==0xFF)
  1623. return (0xFF); /* error */
  1624. debug ("(auto_req)atapi_issue returned sense key %X\n",res);
  1625. memset(sense_ccb,0,sizeof(sense_ccb));
  1626. memset(sense_data,0,sizeof(sense_data));
  1627. sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
  1628. sense_ccb[4]=18; /* allocation Length */
  1629. res=atapi_issue(device,sense_ccb,12,sense_data,18);
  1630. key=(sense_data[2]&0xF);
  1631. asc=(sense_data[12]);
  1632. ascq=(sense_data[13]);
  1633. debug ("ATAPI_CMD_REQ_SENSE returned %x\n",res);
  1634. debug (" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  1635. sense_data[0],
  1636. key,
  1637. asc,
  1638. ascq);
  1639. if((key==0))
  1640. return 0; /* ok device ready */
  1641. if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
  1642. if(unitattn-->0) {
  1643. udelay(200*1000);
  1644. goto retry;
  1645. }
  1646. printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
  1647. goto error;
  1648. }
  1649. if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
  1650. if (notready-->0) {
  1651. udelay(200*1000);
  1652. goto retry;
  1653. }
  1654. printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
  1655. goto error;
  1656. }
  1657. if(asc==0x3a) {
  1658. debug ("Media not present\n");
  1659. goto error;
  1660. }
  1661. #ifdef CONFIG_AMIGAONEG3SE
  1662. if ((sense_data[2]&0xF)==0x0B) {
  1663. debug ("ABORTED COMMAND...retry\n");
  1664. if (retrycnt++ < 4)
  1665. goto retry;
  1666. return (0xFF);
  1667. }
  1668. if ((sense_data[2]&0xf) == 0x02 &&
  1669. sense_data[12] == 0x04 &&
  1670. sense_data[13] == 0x01 ) {
  1671. debug ("Waiting for unit to become active\n");
  1672. udelay(timeout);
  1673. if (retrycnt++ < 4)
  1674. goto retry;
  1675. return 0xFF;
  1676. }
  1677. #endif /* CONFIG_AMIGAONEG3SE */
  1678. printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1679. error:
  1680. debug ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
  1681. return (0xFF);
  1682. }
  1683. static void atapi_inquiry(block_dev_desc_t * dev_desc)
  1684. {
  1685. unsigned char ccb[12]; /* Command descriptor block */
  1686. unsigned char iobuf[64]; /* temp buf */
  1687. unsigned char c;
  1688. int device;
  1689. device=dev_desc->dev;
  1690. dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
  1691. dev_desc->block_read=atapi_read;
  1692. memset(ccb,0,sizeof(ccb));
  1693. memset(iobuf,0,sizeof(iobuf));
  1694. ccb[0]=ATAPI_CMD_INQUIRY;
  1695. ccb[4]=40; /* allocation Legnth */
  1696. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40);
  1697. debug ("ATAPI_CMD_INQUIRY returned %x\n",c);
  1698. if (c!=0)
  1699. return;
  1700. /* copy device ident strings */
  1701. ident_cpy((unsigned char*)dev_desc->vendor,&iobuf[8],8);
  1702. ident_cpy((unsigned char*)dev_desc->product,&iobuf[16],16);
  1703. ident_cpy((unsigned char*)dev_desc->revision,&iobuf[32],5);
  1704. dev_desc->lun=0;
  1705. dev_desc->lba=0;
  1706. dev_desc->blksz=0;
  1707. dev_desc->type=iobuf[0] & 0x1f;
  1708. if ((iobuf[1]&0x80)==0x80)
  1709. dev_desc->removable = 1;
  1710. else
  1711. dev_desc->removable = 0;
  1712. memset(ccb,0,sizeof(ccb));
  1713. memset(iobuf,0,sizeof(iobuf));
  1714. ccb[0]=ATAPI_CMD_START_STOP;
  1715. ccb[4]=0x03; /* start */
  1716. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1717. debug ("ATAPI_CMD_START_STOP returned %x\n",c);
  1718. if (c!=0)
  1719. return;
  1720. memset(ccb,0,sizeof(ccb));
  1721. memset(iobuf,0,sizeof(iobuf));
  1722. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0);
  1723. debug ("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
  1724. if (c!=0)
  1725. return;
  1726. memset(ccb,0,sizeof(ccb));
  1727. memset(iobuf,0,sizeof(iobuf));
  1728. ccb[0]=ATAPI_CMD_READ_CAP;
  1729. c=atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8);
  1730. debug ("ATAPI_CMD_READ_CAP returned %x\n",c);
  1731. if (c!=0)
  1732. return;
  1733. debug ("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  1734. iobuf[0],iobuf[1],iobuf[2],iobuf[3],
  1735. iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
  1736. dev_desc->lba =((unsigned long)iobuf[0]<<24) +
  1737. ((unsigned long)iobuf[1]<<16) +
  1738. ((unsigned long)iobuf[2]<< 8) +
  1739. ((unsigned long)iobuf[3]);
  1740. dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
  1741. ((unsigned long)iobuf[5]<<16) +
  1742. ((unsigned long)iobuf[6]<< 8) +
  1743. ((unsigned long)iobuf[7]);
  1744. #ifdef CONFIG_LBA48
  1745. dev_desc->lba48 = 0; /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
  1746. #endif
  1747. return;
  1748. }
  1749. /*
  1750. * atapi_read:
  1751. * we transfer only one block per command, since the multiple DRQ per
  1752. * command is not yet implemented
  1753. */
  1754. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  1755. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  1756. #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE /* max blocks */
  1757. ulong atapi_read (int device, lbaint_t blknr, ulong blkcnt, void *buffer)
  1758. {
  1759. ulong n = 0;
  1760. unsigned char ccb[12]; /* Command descriptor block */
  1761. ulong cnt;
  1762. debug ("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
  1763. device, blknr, blkcnt, (ulong)buffer);
  1764. do {
  1765. if (blkcnt>ATAPI_READ_MAX_BLOCK) {
  1766. cnt=ATAPI_READ_MAX_BLOCK;
  1767. } else {
  1768. cnt=blkcnt;
  1769. }
  1770. ccb[0]=ATAPI_CMD_READ_12;
  1771. ccb[1]=0; /* reserved */
  1772. ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
  1773. ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /* */
  1774. ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
  1775. ccb[5]=(unsigned char) blknr & 0xFF; /* LSB Block */
  1776. ccb[6]=(unsigned char) (cnt >>24) & 0xFF; /* MSB Block count */
  1777. ccb[7]=(unsigned char) (cnt >>16) & 0xFF;
  1778. ccb[8]=(unsigned char) (cnt >> 8) & 0xFF;
  1779. ccb[9]=(unsigned char) cnt & 0xFF; /* LSB Block */
  1780. ccb[10]=0; /* reserved */
  1781. ccb[11]=0; /* reserved */
  1782. if (atapi_issue_autoreq(device,ccb,12,
  1783. (unsigned char *)buffer,
  1784. cnt*ATAPI_READ_BLOCK_SIZE) == 0xFF) {
  1785. return (n);
  1786. }
  1787. n+=cnt;
  1788. blkcnt-=cnt;
  1789. blknr+=cnt;
  1790. buffer+=(cnt*ATAPI_READ_BLOCK_SIZE);
  1791. } while (blkcnt > 0);
  1792. return (n);
  1793. }
  1794. /* ------------------------------------------------------------------------- */
  1795. #endif /* CONFIG_ATAPI */
  1796. U_BOOT_CMD(
  1797. ide, 5, 1, do_ide,
  1798. "ide - IDE sub-system\n",
  1799. "reset - reset IDE controller\n"
  1800. "ide info - show available IDE devices\n"
  1801. "ide device [dev] - show or set current device\n"
  1802. "ide part [dev] - print partition table of one or all IDE devices\n"
  1803. "ide read addr blk# cnt\n"
  1804. "ide write addr blk# cnt - read/write `cnt'"
  1805. " blocks starting at block `blk#'\n"
  1806. " to/from memory address `addr'\n"
  1807. );
  1808. U_BOOT_CMD(
  1809. diskboot, 3, 1, do_diskboot,
  1810. "diskboot- boot from IDE device\n",
  1811. "loadAddr dev:part\n"
  1812. );