cmd_ide.c 49 KB

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