cmd_ide.c 50 KB

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