cmd_ide.c 50 KB

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