cmd_ide.c 50 KB

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