cmd_ide.c 47 KB

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