cmd_ide.c 51 KB

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