cmd_ide.c 50 KB

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