cmd_ide.c 50 KB

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