ide.c 26 KB

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