ide.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2011
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #define LOG_CATEGORY UCLASS_IDE
  7. #include <common.h>
  8. #include <ata.h>
  9. #include <blk.h>
  10. #include <dm.h>
  11. #include <ide.h>
  12. #include <log.h>
  13. #include <part.h>
  14. #include <watchdog.h>
  15. #include <asm/io.h>
  16. #include <linux/delay.h>
  17. #ifdef __PPC__
  18. # define EIEIO __asm__ volatile ("eieio")
  19. # define SYNC __asm__ volatile ("sync")
  20. #else
  21. # define EIEIO /* nothing */
  22. # define SYNC /* nothing */
  23. #endif
  24. /* Current offset for IDE0 / IDE1 bus access */
  25. ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
  26. #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
  27. CONFIG_SYS_ATA_IDE0_OFFSET,
  28. #endif
  29. #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
  30. CONFIG_SYS_ATA_IDE1_OFFSET,
  31. #endif
  32. };
  33. static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
  34. struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
  35. #define IDE_TIME_OUT 2000 /* 2 sec timeout */
  36. #define ATAPI_TIME_OUT 7000 /* 7 sec timeout (5 sec seems to work...) */
  37. #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
  38. #ifndef CONFIG_SYS_ATA_PORT_ADDR
  39. #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
  40. #endif
  41. #ifdef CONFIG_IDE_RESET
  42. extern void ide_set_reset(int idereset);
  43. static void ide_reset(void)
  44. {
  45. int i;
  46. for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
  47. ide_bus_ok[i] = 0;
  48. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
  49. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  50. ide_set_reset(1); /* assert reset */
  51. /* the reset signal shall be asserted for et least 25 us */
  52. udelay(25);
  53. WATCHDOG_RESET();
  54. /* de-assert RESET signal */
  55. ide_set_reset(0);
  56. /* wait 250 ms */
  57. for (i = 0; i < 250; ++i)
  58. udelay(1000);
  59. }
  60. #else
  61. #define ide_reset() /* dummy */
  62. #endif /* CONFIG_IDE_RESET */
  63. /*
  64. * Wait until Busy bit is off, or timeout (in ms)
  65. * Return last status
  66. */
  67. static uchar ide_wait(int dev, ulong t)
  68. {
  69. ulong delay = 10 * t; /* poll every 100 us */
  70. uchar c;
  71. while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
  72. udelay(100);
  73. if (delay-- == 0)
  74. break;
  75. }
  76. return c;
  77. }
  78. /*
  79. * copy src to dest, skipping leading and trailing blanks and null
  80. * terminate the string
  81. * "len" is the size of available memory including the terminating '\0'
  82. */
  83. static void ident_cpy(unsigned char *dst, unsigned char *src,
  84. unsigned int len)
  85. {
  86. unsigned char *end, *last;
  87. last = dst;
  88. end = src + len - 1;
  89. /* reserve space for '\0' */
  90. if (len < 2)
  91. goto OUT;
  92. /* skip leading white space */
  93. while ((*src) && (src < end) && (*src == ' '))
  94. ++src;
  95. /* copy string, omitting trailing white space */
  96. while ((*src) && (src < end)) {
  97. *dst++ = *src;
  98. if (*src++ != ' ')
  99. last = dst;
  100. }
  101. OUT:
  102. *last = '\0';
  103. }
  104. #ifdef CONFIG_ATAPI
  105. /****************************************************************************
  106. * ATAPI Support
  107. */
  108. /* since ATAPI may use commands with not 4 bytes alligned length
  109. * we have our own transfer functions, 2 bytes alligned */
  110. __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
  111. {
  112. uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  113. ushort *dbuf;
  114. dbuf = (ushort *)sect_buf;
  115. debug("in output data shorts base for read is %p\n", (void *)paddr);
  116. while (shorts--) {
  117. EIEIO;
  118. outw(cpu_to_le16(*dbuf++), paddr);
  119. }
  120. }
  121. __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
  122. {
  123. uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  124. ushort *dbuf;
  125. dbuf = (ushort *)sect_buf;
  126. debug("in input data shorts base for read is %p\n", (void *)paddr);
  127. while (shorts--) {
  128. EIEIO;
  129. *dbuf++ = le16_to_cpu(inw(paddr));
  130. }
  131. }
  132. /*
  133. * Wait until (Status & mask) == res, or timeout (in ms)
  134. * Return last status
  135. * This is used since some ATAPI CD ROMs clears their Busy Bit first
  136. * and then they set their DRQ Bit
  137. */
  138. static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
  139. {
  140. ulong delay = 10 * t; /* poll every 100 us */
  141. uchar c;
  142. /* prevents to read the status before valid */
  143. c = ide_inb(dev, ATA_DEV_CTL);
  144. while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
  145. /* break if error occurs (doesn't make sense to wait more) */
  146. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
  147. break;
  148. udelay(100);
  149. if (delay-- == 0)
  150. break;
  151. }
  152. return c;
  153. }
  154. /*
  155. * issue an atapi command
  156. */
  157. unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
  158. unsigned char *buffer, int buflen)
  159. {
  160. unsigned char c, err, mask, res;
  161. int n;
  162. /* Select device
  163. */
  164. mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
  165. res = 0;
  166. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  167. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  168. if ((c & mask) != res) {
  169. printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
  170. c);
  171. err = 0xFF;
  172. goto AI_OUT;
  173. }
  174. /* write taskfile */
  175. ide_outb(device, ATA_ERROR_REG, 0); /* no DMA, no overlaped */
  176. ide_outb(device, ATA_SECT_CNT, 0);
  177. ide_outb(device, ATA_SECT_NUM, 0);
  178. ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
  179. ide_outb(device, ATA_CYL_HIGH,
  180. (unsigned char) ((buflen >> 8) & 0xFF));
  181. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  182. ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
  183. udelay(50);
  184. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  185. res = ATA_STAT_DRQ;
  186. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  187. if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
  188. printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
  189. device, c);
  190. err = 0xFF;
  191. goto AI_OUT;
  192. }
  193. /* write command block */
  194. ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
  195. /* ATAPI Command written wait for completition */
  196. udelay(5000); /* device must set bsy */
  197. mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
  198. /*
  199. * if no data wait for DRQ = 0 BSY = 0
  200. * if data wait for DRQ = 1 BSY = 0
  201. */
  202. res = 0;
  203. if (buflen)
  204. res = ATA_STAT_DRQ;
  205. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  206. if ((c & mask) != res) {
  207. if (c & ATA_STAT_ERR) {
  208. err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
  209. debug("atapi_issue 1 returned sense key %X status %02X\n",
  210. err, c);
  211. } else {
  212. printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n",
  213. ccb[0], c);
  214. err = 0xFF;
  215. }
  216. goto AI_OUT;
  217. }
  218. n = ide_inb(device, ATA_CYL_HIGH);
  219. n <<= 8;
  220. n += ide_inb(device, ATA_CYL_LOW);
  221. if (n > buflen) {
  222. printf("ERROR, transfer bytes %d requested only %d\n", n,
  223. buflen);
  224. err = 0xff;
  225. goto AI_OUT;
  226. }
  227. if ((n == 0) && (buflen < 0)) {
  228. printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
  229. err = 0xff;
  230. goto AI_OUT;
  231. }
  232. if (n != buflen) {
  233. debug("WARNING, transfer bytes %d not equal with requested %d\n",
  234. n, buflen);
  235. }
  236. if (n != 0) { /* data transfer */
  237. debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
  238. /* we transfer shorts */
  239. n >>= 1;
  240. /* ok now decide if it is an in or output */
  241. if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
  242. debug("Write to device\n");
  243. ide_output_data_shorts(device, (unsigned short *)buffer,
  244. n);
  245. } else {
  246. debug("Read from device @ %p shorts %d\n", buffer, n);
  247. ide_input_data_shorts(device, (unsigned short *)buffer,
  248. n);
  249. }
  250. }
  251. udelay(5000); /* seems that some CD ROMs need this... */
  252. mask = ATA_STAT_BUSY | ATA_STAT_ERR;
  253. res = 0;
  254. c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
  255. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  256. err = (ide_inb(device, ATA_ERROR_REG) >> 4);
  257. debug("atapi_issue 2 returned sense key %X status %X\n", err,
  258. c);
  259. } else {
  260. err = 0;
  261. }
  262. AI_OUT:
  263. return err;
  264. }
  265. /*
  266. * sending the command to atapi_issue. If an status other than good
  267. * returns, an request_sense will be issued
  268. */
  269. #define ATAPI_DRIVE_NOT_READY 100
  270. #define ATAPI_UNIT_ATTN 10
  271. unsigned char atapi_issue_autoreq(int device,
  272. unsigned char *ccb,
  273. int ccblen,
  274. unsigned char *buffer, int buflen)
  275. {
  276. unsigned char sense_data[18], sense_ccb[12];
  277. unsigned char res, key, asc, ascq;
  278. int notready, unitattn;
  279. unitattn = ATAPI_UNIT_ATTN;
  280. notready = ATAPI_DRIVE_NOT_READY;
  281. retry:
  282. res = atapi_issue(device, ccb, ccblen, buffer, buflen);
  283. if (res == 0)
  284. return 0; /* Ok */
  285. if (res == 0xFF)
  286. return 0xFF; /* error */
  287. debug("(auto_req)atapi_issue returned sense key %X\n", res);
  288. memset(sense_ccb, 0, sizeof(sense_ccb));
  289. memset(sense_data, 0, sizeof(sense_data));
  290. sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
  291. sense_ccb[4] = 18; /* allocation Length */
  292. res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
  293. key = (sense_data[2] & 0xF);
  294. asc = (sense_data[12]);
  295. ascq = (sense_data[13]);
  296. debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
  297. debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
  298. sense_data[0], key, asc, ascq);
  299. if ((key == 0))
  300. return 0; /* ok device ready */
  301. if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
  302. if (unitattn-- > 0) {
  303. udelay(200 * 1000);
  304. goto retry;
  305. }
  306. printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
  307. goto error;
  308. }
  309. if ((asc == 0x4) && (ascq == 0x1)) {
  310. /* not ready, but will be ready soon */
  311. if (notready-- > 0) {
  312. udelay(200 * 1000);
  313. goto retry;
  314. }
  315. printf("Drive not ready, tried %d times\n",
  316. ATAPI_DRIVE_NOT_READY);
  317. goto error;
  318. }
  319. if (asc == 0x3a) {
  320. debug("Media not present\n");
  321. goto error;
  322. }
  323. printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
  324. ascq);
  325. error:
  326. debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
  327. return 0xFF;
  328. }
  329. /*
  330. * atapi_read:
  331. * we transfer only one block per command, since the multiple DRQ per
  332. * command is not yet implemented
  333. */
  334. #define ATAPI_READ_MAX_BYTES 2048 /* we read max 2kbytes */
  335. #define ATAPI_READ_BLOCK_SIZE 2048 /* assuming CD part */
  336. #define ATAPI_READ_MAX_BLOCK (ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
  337. ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
  338. void *buffer)
  339. {
  340. int device = block_dev->devnum;
  341. ulong n = 0;
  342. unsigned char ccb[12]; /* Command descriptor block */
  343. ulong cnt;
  344. debug("atapi_read dev %d start " LBAF " blocks " LBAF
  345. " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
  346. do {
  347. if (blkcnt > ATAPI_READ_MAX_BLOCK)
  348. cnt = ATAPI_READ_MAX_BLOCK;
  349. else
  350. cnt = blkcnt;
  351. ccb[0] = ATAPI_CMD_READ_12;
  352. ccb[1] = 0; /* reserved */
  353. ccb[2] = (unsigned char) (blknr >> 24) & 0xFF; /* MSB Block */
  354. ccb[3] = (unsigned char) (blknr >> 16) & 0xFF; /* */
  355. ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
  356. ccb[5] = (unsigned char) blknr & 0xFF; /* LSB Block */
  357. ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
  358. ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
  359. ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
  360. ccb[9] = (unsigned char) cnt & 0xFF; /* LSB Block */
  361. ccb[10] = 0; /* reserved */
  362. ccb[11] = 0; /* reserved */
  363. if (atapi_issue_autoreq(device, ccb, 12,
  364. (unsigned char *)buffer,
  365. cnt * ATAPI_READ_BLOCK_SIZE)
  366. == 0xFF) {
  367. return n;
  368. }
  369. n += cnt;
  370. blkcnt -= cnt;
  371. blknr += cnt;
  372. buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
  373. } while (blkcnt > 0);
  374. return n;
  375. }
  376. static void atapi_inquiry(struct blk_desc *dev_desc)
  377. {
  378. unsigned char ccb[12]; /* Command descriptor block */
  379. unsigned char iobuf[64]; /* temp buf */
  380. unsigned char c;
  381. int device;
  382. device = dev_desc->devnum;
  383. dev_desc->type = DEV_TYPE_UNKNOWN; /* not yet valid */
  384. #ifndef CONFIG_BLK
  385. dev_desc->block_read = atapi_read;
  386. #endif
  387. memset(ccb, 0, sizeof(ccb));
  388. memset(iobuf, 0, sizeof(iobuf));
  389. ccb[0] = ATAPI_CMD_INQUIRY;
  390. ccb[4] = 40; /* allocation Legnth */
  391. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
  392. debug("ATAPI_CMD_INQUIRY returned %x\n", c);
  393. if (c != 0)
  394. return;
  395. /* copy device ident strings */
  396. ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
  397. ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
  398. ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
  399. dev_desc->lun = 0;
  400. dev_desc->lba = 0;
  401. dev_desc->blksz = 0;
  402. dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
  403. dev_desc->type = iobuf[0] & 0x1f;
  404. if ((iobuf[1] & 0x80) == 0x80)
  405. dev_desc->removable = 1;
  406. else
  407. dev_desc->removable = 0;
  408. memset(ccb, 0, sizeof(ccb));
  409. memset(iobuf, 0, sizeof(iobuf));
  410. ccb[0] = ATAPI_CMD_START_STOP;
  411. ccb[4] = 0x03; /* start */
  412. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
  413. debug("ATAPI_CMD_START_STOP returned %x\n", c);
  414. if (c != 0)
  415. return;
  416. memset(ccb, 0, sizeof(ccb));
  417. memset(iobuf, 0, sizeof(iobuf));
  418. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
  419. debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
  420. if (c != 0)
  421. return;
  422. memset(ccb, 0, sizeof(ccb));
  423. memset(iobuf, 0, sizeof(iobuf));
  424. ccb[0] = ATAPI_CMD_READ_CAP;
  425. c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
  426. debug("ATAPI_CMD_READ_CAP returned %x\n", c);
  427. if (c != 0)
  428. return;
  429. debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
  430. iobuf[0], iobuf[1], iobuf[2], iobuf[3],
  431. iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
  432. dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
  433. ((unsigned long) iobuf[1] << 16) +
  434. ((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
  435. dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
  436. ((unsigned long) iobuf[5] << 16) +
  437. ((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
  438. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  439. #ifdef CONFIG_LBA48
  440. /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
  441. dev_desc->lba48 = 0;
  442. #endif
  443. return;
  444. }
  445. #endif /* CONFIG_ATAPI */
  446. static void ide_ident(struct blk_desc *dev_desc)
  447. {
  448. unsigned char c;
  449. hd_driveid_t iop;
  450. #ifdef CONFIG_ATAPI
  451. int retries = 0;
  452. #endif
  453. int device;
  454. device = dev_desc->devnum;
  455. printf(" Device %d: ", device);
  456. /* Select device
  457. */
  458. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  459. dev_desc->if_type = IF_TYPE_IDE;
  460. #ifdef CONFIG_ATAPI
  461. retries = 0;
  462. /* Warning: This will be tricky to read */
  463. while (retries <= 1) {
  464. /* check signature */
  465. if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
  466. (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
  467. (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
  468. (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
  469. /* ATAPI Signature found */
  470. dev_desc->if_type = IF_TYPE_ATAPI;
  471. /*
  472. * Start Ident Command
  473. */
  474. ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
  475. /*
  476. * Wait for completion - ATAPI devices need more time
  477. * to become ready
  478. */
  479. c = ide_wait(device, ATAPI_TIME_OUT);
  480. } else
  481. #endif
  482. {
  483. /*
  484. * Start Ident Command
  485. */
  486. ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
  487. /*
  488. * Wait for completion
  489. */
  490. c = ide_wait(device, IDE_TIME_OUT);
  491. }
  492. if (((c & ATA_STAT_DRQ) == 0) ||
  493. ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
  494. #ifdef CONFIG_ATAPI
  495. {
  496. /*
  497. * Need to soft reset the device
  498. * in case it's an ATAPI...
  499. */
  500. debug("Retrying...\n");
  501. ide_outb(device, ATA_DEV_HD,
  502. ATA_LBA | ATA_DEVICE(device));
  503. udelay(100000);
  504. ide_outb(device, ATA_COMMAND, 0x08);
  505. udelay(500000); /* 500 ms */
  506. }
  507. /*
  508. * Select device
  509. */
  510. ide_outb(device, ATA_DEV_HD,
  511. ATA_LBA | ATA_DEVICE(device));
  512. retries++;
  513. #else
  514. return;
  515. #endif
  516. }
  517. #ifdef CONFIG_ATAPI
  518. else
  519. break;
  520. } /* see above - ugly to read */
  521. if (retries == 2) /* Not found */
  522. return;
  523. #endif
  524. ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
  525. ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
  526. sizeof(dev_desc->revision));
  527. ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
  528. sizeof(dev_desc->vendor));
  529. ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
  530. sizeof(dev_desc->product));
  531. if ((iop.config & 0x0080) == 0x0080)
  532. dev_desc->removable = 1;
  533. else
  534. dev_desc->removable = 0;
  535. #ifdef CONFIG_ATAPI
  536. if (dev_desc->if_type == IF_TYPE_ATAPI) {
  537. atapi_inquiry(dev_desc);
  538. return;
  539. }
  540. #endif /* CONFIG_ATAPI */
  541. iop.lba_capacity[0] = be16_to_cpu(iop.lba_capacity[0]);
  542. iop.lba_capacity[1] = be16_to_cpu(iop.lba_capacity[1]);
  543. dev_desc->lba =
  544. ((unsigned long)iop.lba_capacity[0]) |
  545. ((unsigned long)iop.lba_capacity[1] << 16);
  546. #ifdef CONFIG_LBA48
  547. if (iop.command_set_2 & 0x0400) { /* LBA 48 support */
  548. dev_desc->lba48 = 1;
  549. for (int i = 0; i < 4; i++)
  550. iop.lba48_capacity[i] = be16_to_cpu(iop.lba48_capacity[i]);
  551. dev_desc->lba =
  552. ((unsigned long long)iop.lba48_capacity[0] |
  553. ((unsigned long long)iop.lba48_capacity[1] << 16) |
  554. ((unsigned long long)iop.lba48_capacity[2] << 32) |
  555. ((unsigned long long)iop.lba48_capacity[3] << 48));
  556. } else {
  557. dev_desc->lba48 = 0;
  558. }
  559. #endif /* CONFIG_LBA48 */
  560. /* assuming HD */
  561. dev_desc->type = DEV_TYPE_HARDDISK;
  562. dev_desc->blksz = ATA_BLOCKSIZE;
  563. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  564. dev_desc->lun = 0; /* just to fill something in... */
  565. #if 0 /* only used to test the powersaving mode,
  566. * if enabled, the drive goes after 5 sec
  567. * in standby mode */
  568. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  569. c = ide_wait(device, IDE_TIME_OUT);
  570. ide_outb(device, ATA_SECT_CNT, 1);
  571. ide_outb(device, ATA_LBA_LOW, 0);
  572. ide_outb(device, ATA_LBA_MID, 0);
  573. ide_outb(device, ATA_LBA_HIGH, 0);
  574. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  575. ide_outb(device, ATA_COMMAND, 0xe3);
  576. udelay(50);
  577. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  578. #endif
  579. }
  580. __weak void ide_outb(int dev, int port, unsigned char val)
  581. {
  582. debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
  583. dev, port, val,
  584. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  585. #if defined(CONFIG_IDE_AHB)
  586. if (port) {
  587. /* write command */
  588. ide_write_register(dev, port, val);
  589. } else {
  590. /* write data */
  591. outb(val, (ATA_CURR_BASE(dev)));
  592. }
  593. #else
  594. outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  595. #endif
  596. }
  597. __weak unsigned char ide_inb(int dev, int port)
  598. {
  599. uchar val;
  600. #if defined(CONFIG_IDE_AHB)
  601. val = ide_read_register(dev, port);
  602. #else
  603. val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
  604. #endif
  605. debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
  606. dev, port,
  607. (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
  608. return val;
  609. }
  610. void ide_init(void)
  611. {
  612. unsigned char c;
  613. int i, bus;
  614. #ifdef CONFIG_IDE_PREINIT
  615. WATCHDOG_RESET();
  616. if (ide_preinit()) {
  617. puts("ide_preinit failed\n");
  618. return;
  619. }
  620. #endif /* CONFIG_IDE_PREINIT */
  621. WATCHDOG_RESET();
  622. /* ATAPI Drives seems to need a proper IDE Reset */
  623. ide_reset();
  624. /*
  625. * Wait for IDE to get ready.
  626. * According to spec, this can take up to 31 seconds!
  627. */
  628. for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
  629. int dev =
  630. bus * (CONFIG_SYS_IDE_MAXDEVICE /
  631. CONFIG_SYS_IDE_MAXBUS);
  632. printf("Bus %d: ", bus);
  633. ide_bus_ok[bus] = 0;
  634. /* Select device
  635. */
  636. udelay(100000); /* 100 ms */
  637. ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
  638. udelay(100000); /* 100 ms */
  639. i = 0;
  640. do {
  641. udelay(10000); /* 10 ms */
  642. c = ide_inb(dev, ATA_STATUS);
  643. i++;
  644. if (i > (ATA_RESET_TIME * 100)) {
  645. puts("** Timeout **\n");
  646. return;
  647. }
  648. if ((i >= 100) && ((i % 100) == 0))
  649. putc('.');
  650. } while (c & ATA_STAT_BUSY);
  651. if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
  652. puts("not available ");
  653. debug("Status = 0x%02X ", c);
  654. #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
  655. } else if ((c & ATA_STAT_READY) == 0) {
  656. puts("not available ");
  657. debug("Status = 0x%02X ", c);
  658. #endif
  659. } else {
  660. puts("OK ");
  661. ide_bus_ok[bus] = 1;
  662. }
  663. WATCHDOG_RESET();
  664. }
  665. putc('\n');
  666. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
  667. ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  668. ide_dev_desc[i].if_type = IF_TYPE_IDE;
  669. ide_dev_desc[i].devnum = i;
  670. ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
  671. ide_dev_desc[i].blksz = 0;
  672. ide_dev_desc[i].log2blksz =
  673. LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
  674. ide_dev_desc[i].lba = 0;
  675. #ifndef CONFIG_BLK
  676. ide_dev_desc[i].block_read = ide_read;
  677. ide_dev_desc[i].block_write = ide_write;
  678. #endif
  679. if (!ide_bus_ok[IDE_BUS(i)])
  680. continue;
  681. ide_ident(&ide_dev_desc[i]);
  682. dev_print(&ide_dev_desc[i]);
  683. #ifndef CONFIG_BLK
  684. if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
  685. /* initialize partition type */
  686. part_init(&ide_dev_desc[i]);
  687. }
  688. #endif
  689. }
  690. WATCHDOG_RESET();
  691. #ifdef CONFIG_BLK
  692. struct udevice *dev;
  693. uclass_first_device(UCLASS_IDE, &dev);
  694. #endif
  695. }
  696. __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
  697. {
  698. uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  699. ushort *dbuf = (ushort *)sect_buf;
  700. debug("in input swap data base for read is %p\n", (void *)paddr);
  701. while (words--) {
  702. EIEIO;
  703. *dbuf++ = be16_to_cpu(inw(paddr));
  704. EIEIO;
  705. *dbuf++ = be16_to_cpu(inw(paddr));
  706. }
  707. }
  708. __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
  709. {
  710. #if defined(CONFIG_IDE_AHB)
  711. ide_write_data(dev, sect_buf, words);
  712. #else
  713. uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  714. ushort *dbuf;
  715. dbuf = (ushort *)sect_buf;
  716. while (words--) {
  717. EIEIO;
  718. outw(cpu_to_le16(*dbuf++), paddr);
  719. EIEIO;
  720. outw(cpu_to_le16(*dbuf++), paddr);
  721. }
  722. #endif /* CONFIG_IDE_AHB */
  723. }
  724. __weak void ide_input_data(int dev, ulong *sect_buf, int words)
  725. {
  726. #if defined(CONFIG_IDE_AHB)
  727. ide_read_data(dev, sect_buf, words);
  728. #else
  729. uintptr_t paddr = (ATA_CURR_BASE(dev) + ATA_DATA_REG);
  730. ushort *dbuf;
  731. dbuf = (ushort *)sect_buf;
  732. debug("in input data base for read is %p\n", (void *)paddr);
  733. while (words--) {
  734. EIEIO;
  735. *dbuf++ = le16_to_cpu(inw(paddr));
  736. EIEIO;
  737. *dbuf++ = le16_to_cpu(inw(paddr));
  738. }
  739. #endif /* CONFIG_IDE_AHB */
  740. }
  741. #ifdef CONFIG_BLK
  742. ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  743. void *buffer)
  744. #else
  745. ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
  746. void *buffer)
  747. #endif
  748. {
  749. #ifdef CONFIG_BLK
  750. struct blk_desc *block_dev = dev_get_uclass_plat(dev);
  751. #endif
  752. int device = block_dev->devnum;
  753. ulong n = 0;
  754. unsigned char c;
  755. unsigned char pwrsave = 0; /* power save */
  756. #ifdef CONFIG_LBA48
  757. unsigned char lba48 = 0;
  758. if (blknr & 0x0000fffff0000000ULL) {
  759. /* more than 28 bits used, use 48bit mode */
  760. lba48 = 1;
  761. }
  762. #endif
  763. debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
  764. device, blknr, blkcnt, (ulong) buffer);
  765. /* Select device
  766. */
  767. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  768. c = ide_wait(device, IDE_TIME_OUT);
  769. if (c & ATA_STAT_BUSY) {
  770. printf("IDE read: device %d not ready\n", device);
  771. goto IDE_READ_E;
  772. }
  773. /* first check if the drive is in Powersaving mode, if yes,
  774. * increase the timeout value */
  775. ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
  776. udelay(50);
  777. c = ide_wait(device, IDE_TIME_OUT); /* can't take over 500 ms */
  778. if (c & ATA_STAT_BUSY) {
  779. printf("IDE read: device %d not ready\n", device);
  780. goto IDE_READ_E;
  781. }
  782. if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
  783. printf("No Powersaving mode %X\n", c);
  784. } else {
  785. c = ide_inb(device, ATA_SECT_CNT);
  786. debug("Powersaving %02X\n", c);
  787. if (c == 0)
  788. pwrsave = 1;
  789. }
  790. while (blkcnt-- > 0) {
  791. c = ide_wait(device, IDE_TIME_OUT);
  792. if (c & ATA_STAT_BUSY) {
  793. printf("IDE read: device %d not ready\n", device);
  794. break;
  795. }
  796. #ifdef CONFIG_LBA48
  797. if (lba48) {
  798. /* write high bits */
  799. ide_outb(device, ATA_SECT_CNT, 0);
  800. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  801. #ifdef CONFIG_SYS_64BIT_LBA
  802. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  803. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  804. #else
  805. ide_outb(device, ATA_LBA_MID, 0);
  806. ide_outb(device, ATA_LBA_HIGH, 0);
  807. #endif
  808. }
  809. #endif
  810. ide_outb(device, ATA_SECT_CNT, 1);
  811. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  812. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  813. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  814. #ifdef CONFIG_LBA48
  815. if (lba48) {
  816. ide_outb(device, ATA_DEV_HD,
  817. ATA_LBA | ATA_DEVICE(device));
  818. ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ_EXT);
  819. } else
  820. #endif
  821. {
  822. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  823. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  824. ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_READ);
  825. }
  826. udelay(50);
  827. if (pwrsave) {
  828. /* may take up to 4 sec */
  829. c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
  830. pwrsave = 0;
  831. } else {
  832. /* can't take over 500 ms */
  833. c = ide_wait(device, IDE_TIME_OUT);
  834. }
  835. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  836. ATA_STAT_DRQ) {
  837. printf("Error (no IRQ) dev %d blk " LBAF
  838. ": status %#02x\n", device, blknr, c);
  839. break;
  840. }
  841. ide_input_data(device, buffer, ATA_SECTORWORDS);
  842. (void) ide_inb(device, ATA_STATUS); /* clear IRQ */
  843. ++n;
  844. ++blknr;
  845. buffer += ATA_BLOCKSIZE;
  846. }
  847. IDE_READ_E:
  848. return n;
  849. }
  850. #ifdef CONFIG_BLK
  851. ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
  852. const void *buffer)
  853. #else
  854. ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
  855. const void *buffer)
  856. #endif
  857. {
  858. #ifdef CONFIG_BLK
  859. struct blk_desc *block_dev = dev_get_uclass_plat(dev);
  860. #endif
  861. int device = block_dev->devnum;
  862. ulong n = 0;
  863. unsigned char c;
  864. #ifdef CONFIG_LBA48
  865. unsigned char lba48 = 0;
  866. if (blknr & 0x0000fffff0000000ULL) {
  867. /* more than 28 bits used, use 48bit mode */
  868. lba48 = 1;
  869. }
  870. #endif
  871. /* Select device
  872. */
  873. ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
  874. while (blkcnt-- > 0) {
  875. c = ide_wait(device, IDE_TIME_OUT);
  876. if (c & ATA_STAT_BUSY) {
  877. printf("IDE read: device %d not ready\n", device);
  878. goto WR_OUT;
  879. }
  880. #ifdef CONFIG_LBA48
  881. if (lba48) {
  882. /* write high bits */
  883. ide_outb(device, ATA_SECT_CNT, 0);
  884. ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
  885. #ifdef CONFIG_SYS_64BIT_LBA
  886. ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
  887. ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
  888. #else
  889. ide_outb(device, ATA_LBA_MID, 0);
  890. ide_outb(device, ATA_LBA_HIGH, 0);
  891. #endif
  892. }
  893. #endif
  894. ide_outb(device, ATA_SECT_CNT, 1);
  895. ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
  896. ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
  897. ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
  898. #ifdef CONFIG_LBA48
  899. if (lba48) {
  900. ide_outb(device, ATA_DEV_HD,
  901. ATA_LBA | ATA_DEVICE(device));
  902. ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE_EXT);
  903. } else
  904. #endif
  905. {
  906. ide_outb(device, ATA_DEV_HD, ATA_LBA |
  907. ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
  908. ide_outb(device, ATA_COMMAND, ATA_CMD_PIO_WRITE);
  909. }
  910. udelay(50);
  911. /* can't take over 500 ms */
  912. c = ide_wait(device, IDE_TIME_OUT);
  913. if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
  914. ATA_STAT_DRQ) {
  915. printf("Error (no IRQ) dev %d blk " LBAF
  916. ": status %#02x\n", device, blknr, c);
  917. goto WR_OUT;
  918. }
  919. ide_output_data(device, buffer, ATA_SECTORWORDS);
  920. c = ide_inb(device, ATA_STATUS); /* clear IRQ */
  921. ++n;
  922. ++blknr;
  923. buffer += ATA_BLOCKSIZE;
  924. }
  925. WR_OUT:
  926. return n;
  927. }
  928. #if defined(CONFIG_OF_IDE_FIXUP)
  929. int ide_device_present(int dev)
  930. {
  931. if (dev >= CONFIG_SYS_IDE_MAXBUS)
  932. return 0;
  933. return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
  934. }
  935. #endif
  936. #ifdef CONFIG_BLK
  937. static int ide_blk_probe(struct udevice *udev)
  938. {
  939. struct blk_desc *desc = dev_get_uclass_plat(udev);
  940. /* fill in device vendor/product/rev strings */
  941. strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
  942. BLK_VEN_SIZE);
  943. desc->vendor[BLK_VEN_SIZE] = '\0';
  944. strncpy(desc->product, ide_dev_desc[desc->devnum].product,
  945. BLK_PRD_SIZE);
  946. desc->product[BLK_PRD_SIZE] = '\0';
  947. strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
  948. BLK_REV_SIZE);
  949. desc->revision[BLK_REV_SIZE] = '\0';
  950. return 0;
  951. }
  952. static const struct blk_ops ide_blk_ops = {
  953. .read = ide_read,
  954. .write = ide_write,
  955. };
  956. U_BOOT_DRIVER(ide_blk) = {
  957. .name = "ide_blk",
  958. .id = UCLASS_BLK,
  959. .ops = &ide_blk_ops,
  960. .probe = ide_blk_probe,
  961. };
  962. static int ide_probe(struct udevice *udev)
  963. {
  964. struct udevice *blk_dev;
  965. char name[20];
  966. int blksz;
  967. lbaint_t size;
  968. int i;
  969. int ret;
  970. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
  971. if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
  972. sprintf(name, "blk#%d", i);
  973. blksz = ide_dev_desc[i].blksz;
  974. size = blksz * ide_dev_desc[i].lba;
  975. /*
  976. * With CDROM, if there is no CD inserted, blksz will
  977. * be zero, don't bother to create IDE block device.
  978. */
  979. if (!blksz)
  980. continue;
  981. ret = blk_create_devicef(udev, "ide_blk", name,
  982. IF_TYPE_IDE, i,
  983. blksz, size, &blk_dev);
  984. if (ret)
  985. return ret;
  986. }
  987. }
  988. return 0;
  989. }
  990. U_BOOT_DRIVER(ide) = {
  991. .name = "ide",
  992. .id = UCLASS_IDE,
  993. .probe = ide_probe,
  994. };
  995. struct pci_device_id ide_supported[] = {
  996. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
  997. { }
  998. };
  999. U_BOOT_PCI_DEVICE(ide, ide_supported);
  1000. UCLASS_DRIVER(ide) = {
  1001. .name = "ide",
  1002. .id = UCLASS_IDE,
  1003. };
  1004. #else
  1005. U_BOOT_LEGACY_BLK(ide) = {
  1006. .if_typename = "ide",
  1007. .if_type = IF_TYPE_IDE,
  1008. .max_devs = CONFIG_SYS_IDE_MAXDEVICE,
  1009. .desc = ide_dev_desc,
  1010. };
  1011. #endif