mg_disk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * (C) Copyright 2009 mGine co.
  3. * unsik Kim <donari75@gmail.com>
  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. #include <common.h>
  24. #include <malloc.h>
  25. #include <part.h>
  26. #include <ata.h>
  27. #include <asm/io.h>
  28. #include "mg_disk_prv.h"
  29. #ifndef CONFIG_MG_DISK_RES
  30. #define CONFIG_MG_DISK_RES 0
  31. #endif
  32. #define MG_RES_SEC ((CONFIG_MG_DISK_RES) << 1)
  33. static struct mg_host host;
  34. static inline u32 mg_base(void)
  35. {
  36. return host.drv_data->base;
  37. }
  38. static block_dev_desc_t mg_disk_dev = {
  39. .if_type = IF_TYPE_ATAPI,
  40. .part_type = PART_TYPE_UNKNOWN,
  41. .type = DEV_TYPE_HARDDISK,
  42. .blksz = MG_SECTOR_SIZE,
  43. .priv = &host };
  44. static void mg_dump_status (const char *msg, unsigned int stat, unsigned err)
  45. {
  46. char *name = MG_DEV_NAME;
  47. printf("%s: %s: status=0x%02x { ", name, msg, stat & 0xff);
  48. if (stat & MG_REG_STATUS_BIT_BUSY)
  49. printf("Busy ");
  50. if (stat & MG_REG_STATUS_BIT_READY)
  51. printf("DriveReady ");
  52. if (stat & MG_REG_STATUS_BIT_WRITE_FAULT)
  53. printf("WriteFault ");
  54. if (stat & MG_REG_STATUS_BIT_SEEK_DONE)
  55. printf("SeekComplete ");
  56. if (stat & MG_REG_STATUS_BIT_DATA_REQ)
  57. printf("DataRequest ");
  58. if (stat & MG_REG_STATUS_BIT_CORRECTED_ERROR)
  59. printf("CorrectedError ");
  60. if (stat & MG_REG_STATUS_BIT_ERROR)
  61. printf("Error ");
  62. printf("}\n");
  63. if ((stat & MG_REG_STATUS_BIT_ERROR)) {
  64. printf("%s: %s: error=0x%02x { ", name, msg, err & 0xff);
  65. if (err & MG_REG_ERR_BBK)
  66. printf("BadSector ");
  67. if (err & MG_REG_ERR_UNC)
  68. printf("UncorrectableError ");
  69. if (err & MG_REG_ERR_IDNF)
  70. printf("SectorIdNotFound ");
  71. if (err & MG_REG_ERR_ABRT)
  72. printf("DriveStatusError ");
  73. if (err & MG_REG_ERR_AMNF)
  74. printf("AddrMarkNotFound ");
  75. printf("}\n");
  76. }
  77. }
  78. static unsigned int mg_wait (u32 expect, u32 msec)
  79. {
  80. u8 status;
  81. u32 from, cur, err;
  82. err = MG_ERR_NONE;
  83. reset_timer();
  84. from = get_timer(0);
  85. status = readb(mg_base() + MG_REG_STATUS);
  86. do {
  87. cur = get_timer(from);
  88. if (status & MG_REG_STATUS_BIT_BUSY) {
  89. if (expect == MG_REG_STATUS_BIT_BUSY)
  90. break;
  91. } else {
  92. /* Check the error condition! */
  93. if (status & MG_REG_STATUS_BIT_ERROR) {
  94. err = readb(mg_base() + MG_REG_ERROR);
  95. mg_dump_status("mg_wait", status, err);
  96. break;
  97. }
  98. if (expect == MG_STAT_READY)
  99. if (MG_READY_OK(status))
  100. break;
  101. if (expect == MG_REG_STATUS_BIT_DATA_REQ)
  102. if (status & MG_REG_STATUS_BIT_DATA_REQ)
  103. break;
  104. }
  105. status = readb(mg_base() + MG_REG_STATUS);
  106. } while (cur < msec);
  107. if (cur >= msec)
  108. err = MG_ERR_TIMEOUT;
  109. return err;
  110. }
  111. static int mg_get_disk_id (void)
  112. {
  113. u16 id[(MG_SECTOR_SIZE / sizeof(u16))];
  114. hd_driveid_t *iop = (hd_driveid_t *)id;
  115. u32 i, err, res;
  116. writeb(MG_CMD_ID, mg_base() + MG_REG_COMMAND);
  117. err = mg_wait(MG_REG_STATUS_BIT_DATA_REQ, 3000);
  118. if (err)
  119. return err;
  120. for(i = 0; i < (MG_SECTOR_SIZE / sizeof(u16)); i++)
  121. id[i] = readw(mg_base() + MG_BUFF_OFFSET + i * 2);
  122. writeb(MG_CMD_RD_CONF, mg_base() + MG_REG_COMMAND);
  123. err = mg_wait(MG_STAT_READY, 3000);
  124. if (err)
  125. return err;
  126. ata_swap_buf_le16(id, MG_SECTOR_SIZE / sizeof(u16));
  127. if((iop->field_valid & 1) == 0)
  128. return MG_ERR_TRANSLATION;
  129. ata_id_c_string(id, (unsigned char *)mg_disk_dev.revision,
  130. ATA_ID_FW_REV, sizeof(mg_disk_dev.revision));
  131. ata_id_c_string(id, (unsigned char *)mg_disk_dev.vendor,
  132. ATA_ID_PROD, sizeof(mg_disk_dev.vendor));
  133. ata_id_c_string(id, (unsigned char *)mg_disk_dev.product,
  134. ATA_ID_SERNO, sizeof(mg_disk_dev.product));
  135. #ifdef __BIG_ENDIAN
  136. iop->lba_capacity = (iop->lba_capacity << 16) |
  137. (iop->lba_capacity >> 16);
  138. #endif /* __BIG_ENDIAN */
  139. if (MG_RES_SEC) {
  140. MG_DBG("MG_RES_SEC=%d\n", MG_RES_SEC);
  141. iop->cyls = (iop->lba_capacity - MG_RES_SEC) /
  142. iop->sectors / iop->heads;
  143. res = iop->lba_capacity -
  144. iop->cyls * iop->heads * iop->sectors;
  145. iop->lba_capacity -= res;
  146. printf("mg_disk: %d sectors reserved\n", res);
  147. }
  148. mg_disk_dev.lba = iop->lba_capacity;
  149. return MG_ERR_NONE;
  150. }
  151. static int mg_disk_reset (void)
  152. {
  153. struct mg_drv_data *prv_data = host.drv_data;
  154. s32 err;
  155. u8 init_status;
  156. /* hdd rst low */
  157. prv_data->mg_hdrst_pin(0);
  158. err = mg_wait(MG_REG_STATUS_BIT_BUSY, 300);
  159. if(err)
  160. return err;
  161. /* hdd rst high */
  162. prv_data->mg_hdrst_pin(1);
  163. err = mg_wait(MG_STAT_READY, 3000);
  164. if(err)
  165. return err;
  166. /* soft reset on */
  167. writeb(MG_REG_CTRL_RESET | MG_REG_CTRL_INTR_DISABLE,
  168. mg_base() + MG_REG_DRV_CTRL);
  169. err = mg_wait(MG_REG_STATUS_BIT_BUSY, 3000);
  170. if(err)
  171. return err;
  172. /* soft reset off */
  173. writeb(MG_REG_CTRL_INTR_DISABLE, mg_base() + MG_REG_DRV_CTRL);
  174. err = mg_wait(MG_STAT_READY, 3000);
  175. if(err)
  176. return err;
  177. init_status = readb(mg_base() + MG_REG_STATUS) & 0xf;
  178. if (init_status == 0xf)
  179. return MG_ERR_INIT_STAT;
  180. return err;
  181. }
  182. static unsigned int mg_out(unsigned int sect_num,
  183. unsigned int sect_cnt,
  184. unsigned int cmd)
  185. {
  186. u32 err = MG_ERR_NONE;
  187. err = mg_wait(MG_STAT_READY, 3000);
  188. if (err)
  189. return err;
  190. writeb((u8)sect_cnt, mg_base() + MG_REG_SECT_CNT);
  191. writeb((u8)sect_num, mg_base() + MG_REG_SECT_NUM);
  192. writeb((u8)(sect_num >> 8), mg_base() + MG_REG_CYL_LOW);
  193. writeb((u8)(sect_num >> 16), mg_base() + MG_REG_CYL_HIGH);
  194. writeb((u8)((sect_num >> 24) | MG_REG_HEAD_LBA_MODE),
  195. mg_base() + MG_REG_DRV_HEAD);
  196. writeb(cmd, mg_base() + MG_REG_COMMAND);
  197. return err;
  198. }
  199. static unsigned int mg_do_read_sects(void *buff, u32 sect_num, u32 sect_cnt)
  200. {
  201. u32 i, j, err;
  202. u8 *buff_ptr = buff;
  203. union mg_uniwb uniwb;
  204. err = mg_out(sect_num, sect_cnt, MG_CMD_RD);
  205. if (err)
  206. return err;
  207. for (i = 0; i < sect_cnt; i++) {
  208. err = mg_wait(MG_REG_STATUS_BIT_DATA_REQ, 3000);
  209. if (err)
  210. return err;
  211. if ((u32)buff_ptr & 1) {
  212. for (j = 0; j < MG_SECTOR_SIZE >> 1; j++) {
  213. uniwb.w = readw(mg_base() + MG_BUFF_OFFSET
  214. + (j << 1));
  215. *buff_ptr++ = uniwb.b[0];
  216. *buff_ptr++ = uniwb.b[1];
  217. }
  218. } else {
  219. for(j = 0; j < MG_SECTOR_SIZE >> 1; j++) {
  220. *(u16 *)buff_ptr = readw(mg_base() +
  221. MG_BUFF_OFFSET + (j << 1));
  222. buff_ptr += 2;
  223. }
  224. }
  225. writeb(MG_CMD_RD_CONF, mg_base() + MG_REG_COMMAND);
  226. MG_DBG("%u (0x%8.8x) sector read", sect_num + i,
  227. (sect_num + i) * MG_SECTOR_SIZE);
  228. }
  229. return err;
  230. }
  231. unsigned int mg_disk_read_sects(void *buff, u32 sect_num, u32 sect_cnt)
  232. {
  233. u32 quotient, residue, i, err;
  234. u8 *buff_ptr = buff;
  235. quotient = sect_cnt >> 8;
  236. residue = sect_cnt % 256;
  237. for (i = 0; i < quotient; i++) {
  238. MG_DBG("sect num : %u buff : 0x%8.8x", sect_num, (u32)buff_ptr);
  239. err = mg_do_read_sects(buff_ptr, sect_num, 256);
  240. if (err)
  241. return err;
  242. sect_num += 256;
  243. buff_ptr += 256 * MG_SECTOR_SIZE;
  244. }
  245. if (residue) {
  246. MG_DBG("sect num : %u buff : %8.8x", sect_num, (u32)buff_ptr);
  247. err = mg_do_read_sects(buff_ptr, sect_num, residue);
  248. }
  249. return err;
  250. }
  251. unsigned long mg_block_read (int dev, unsigned long start,
  252. lbaint_t blkcnt, void *buffer)
  253. {
  254. start += MG_RES_SEC;
  255. if (! mg_disk_read_sects(buffer, start, blkcnt))
  256. return blkcnt;
  257. else
  258. return 0;
  259. }
  260. unsigned int mg_disk_read (u32 addr, u8 *buff, u32 len)
  261. {
  262. u8 *sect_buff, *buff_ptr = buff;
  263. u32 cur_addr, next_sec_addr, end_addr, cnt, sect_num;
  264. u32 err = MG_ERR_NONE;
  265. /* TODO : sanity chk */
  266. cnt = 0;
  267. cur_addr = addr;
  268. end_addr = addr + len;
  269. sect_buff = malloc(MG_SECTOR_SIZE);
  270. if (cur_addr & MG_SECTOR_SIZE_MASK) {
  271. next_sec_addr = (cur_addr + MG_SECTOR_SIZE) &
  272. ~MG_SECTOR_SIZE_MASK;
  273. sect_num = cur_addr >> MG_SECTOR_SIZE_SHIFT;
  274. err = mg_disk_read_sects(sect_buff, sect_num, 1);
  275. if (err)
  276. goto mg_read_exit;
  277. if (end_addr < next_sec_addr) {
  278. memcpy(buff_ptr,
  279. sect_buff + (cur_addr & MG_SECTOR_SIZE_MASK),
  280. end_addr - cur_addr);
  281. MG_DBG("copies %u byte from sector offset 0x%8.8x",
  282. end_addr - cur_addr, cur_addr);
  283. cur_addr = end_addr;
  284. } else {
  285. memcpy(buff_ptr,
  286. sect_buff + (cur_addr & MG_SECTOR_SIZE_MASK),
  287. next_sec_addr - cur_addr);
  288. MG_DBG("copies %u byte from sector offset 0x%8.8x",
  289. next_sec_addr - cur_addr, cur_addr);
  290. buff_ptr += (next_sec_addr - cur_addr);
  291. cur_addr = next_sec_addr;
  292. }
  293. }
  294. if (cur_addr < end_addr) {
  295. sect_num = cur_addr >> MG_SECTOR_SIZE_SHIFT;
  296. cnt = ((end_addr & ~MG_SECTOR_SIZE_MASK) - cur_addr) >>
  297. MG_SECTOR_SIZE_SHIFT;
  298. if (cnt)
  299. err = mg_disk_read_sects(buff_ptr, sect_num, cnt);
  300. if (err)
  301. goto mg_read_exit;
  302. buff_ptr += cnt * MG_SECTOR_SIZE;
  303. cur_addr += cnt * MG_SECTOR_SIZE;
  304. if (cur_addr < end_addr) {
  305. sect_num = cur_addr >> MG_SECTOR_SIZE_SHIFT;
  306. err = mg_disk_read_sects(sect_buff, sect_num, 1);
  307. if (err)
  308. goto mg_read_exit;
  309. memcpy(buff_ptr, sect_buff, end_addr - cur_addr);
  310. MG_DBG("copies %u byte", end_addr - cur_addr);
  311. }
  312. }
  313. mg_read_exit:
  314. free(sect_buff);
  315. return err;
  316. }
  317. static int mg_do_write_sects(void *buff, u32 sect_num, u32 sect_cnt)
  318. {
  319. u32 i, j, err;
  320. u8 *buff_ptr = buff;
  321. union mg_uniwb uniwb;
  322. err = mg_out(sect_num, sect_cnt, MG_CMD_WR);
  323. if (err)
  324. return err;
  325. for (i = 0; i < sect_cnt; i++) {
  326. err = mg_wait(MG_REG_STATUS_BIT_DATA_REQ, 3000);
  327. if (err)
  328. return err;
  329. if ((u32)buff_ptr & 1) {
  330. uniwb.b[0] = *buff_ptr++;
  331. uniwb.b[1] = *buff_ptr++;
  332. writew(uniwb.w, mg_base() + MG_BUFF_OFFSET + (j << 1));
  333. } else {
  334. for(j = 0; j < MG_SECTOR_SIZE >> 1; j++) {
  335. writew(*(u16 *)buff_ptr,
  336. mg_base() + MG_BUFF_OFFSET +
  337. (j << 1));
  338. buff_ptr += 2;
  339. }
  340. }
  341. writeb(MG_CMD_WR_CONF, mg_base() + MG_REG_COMMAND);
  342. MG_DBG("%u (0x%8.8x) sector write",
  343. sect_num + i, (sect_num + i) * MG_SECTOR_SIZE);
  344. }
  345. return err;
  346. }
  347. unsigned int mg_disk_write_sects(void *buff, u32 sect_num, u32 sect_cnt)
  348. {
  349. u32 quotient, residue, i;
  350. u32 err = MG_ERR_NONE;
  351. u8 *buff_ptr = buff;
  352. quotient = sect_cnt >> 8;
  353. residue = sect_cnt % 256;
  354. for (i = 0; i < quotient; i++) {
  355. MG_DBG("sect num : %u buff : %8.8x", sect_num, (u32)buff_ptr);
  356. err = mg_do_write_sects(buff_ptr, sect_num, 256);
  357. if (err)
  358. return err;
  359. sect_num += 256;
  360. buff_ptr += 256 * MG_SECTOR_SIZE;
  361. }
  362. if (residue) {
  363. MG_DBG("sect num : %u buff : %8.8x", sect_num, (u32)buff_ptr);
  364. err = mg_do_write_sects(buff_ptr, sect_num, residue);
  365. }
  366. return err;
  367. }
  368. unsigned long mg_block_write (int dev, unsigned long start,
  369. lbaint_t blkcnt, const void *buffer)
  370. {
  371. start += MG_RES_SEC;
  372. if (!mg_disk_write_sects((void *)buffer, start, blkcnt))
  373. return blkcnt;
  374. else
  375. return 0;
  376. }
  377. unsigned int mg_disk_write(u32 addr, u8 *buff, u32 len)
  378. {
  379. u8 *sect_buff, *buff_ptr = buff;
  380. u32 cur_addr, next_sec_addr, end_addr, cnt, sect_num;
  381. u32 err = MG_ERR_NONE;
  382. /* TODO : sanity chk */
  383. cnt = 0;
  384. cur_addr = addr;
  385. end_addr = addr + len;
  386. sect_buff = malloc(MG_SECTOR_SIZE);
  387. if (cur_addr & MG_SECTOR_SIZE_MASK) {
  388. next_sec_addr = (cur_addr + MG_SECTOR_SIZE) &
  389. ~MG_SECTOR_SIZE_MASK;
  390. sect_num = cur_addr >> MG_SECTOR_SIZE_SHIFT;
  391. err = mg_disk_read_sects(sect_buff, sect_num, 1);
  392. if (err)
  393. goto mg_write_exit;
  394. if (end_addr < next_sec_addr) {
  395. memcpy(sect_buff + (cur_addr & MG_SECTOR_SIZE_MASK),
  396. buff_ptr, end_addr - cur_addr);
  397. MG_DBG("copies %u byte to sector offset 0x%8.8x",
  398. end_addr - cur_addr, cur_addr);
  399. cur_addr = end_addr;
  400. } else {
  401. memcpy(sect_buff + (cur_addr & MG_SECTOR_SIZE_MASK),
  402. buff_ptr, next_sec_addr - cur_addr);
  403. MG_DBG("copies %u byte to sector offset 0x%8.8x",
  404. next_sec_addr - cur_addr, cur_addr);
  405. buff_ptr += (next_sec_addr - cur_addr);
  406. cur_addr = next_sec_addr;
  407. }
  408. err = mg_disk_write_sects(sect_buff, sect_num, 1);
  409. if (err)
  410. goto mg_write_exit;
  411. }
  412. if (cur_addr < end_addr) {
  413. sect_num = cur_addr >> MG_SECTOR_SIZE_SHIFT;
  414. cnt = ((end_addr & ~MG_SECTOR_SIZE_MASK) - cur_addr) >>
  415. MG_SECTOR_SIZE_SHIFT;
  416. if (cnt)
  417. err = mg_disk_write_sects(buff_ptr, sect_num, cnt);
  418. if (err)
  419. goto mg_write_exit;
  420. buff_ptr += cnt * MG_SECTOR_SIZE;
  421. cur_addr += cnt * MG_SECTOR_SIZE;
  422. if (cur_addr < end_addr) {
  423. sect_num = cur_addr >> MG_SECTOR_SIZE_SHIFT;
  424. err = mg_disk_read_sects(sect_buff, sect_num, 1);
  425. if (err)
  426. goto mg_write_exit;
  427. memcpy(sect_buff, buff_ptr, end_addr - cur_addr);
  428. MG_DBG("copies %u byte", end_addr - cur_addr);
  429. err = mg_disk_write_sects(sect_buff, sect_num, 1);
  430. }
  431. }
  432. mg_write_exit:
  433. free(sect_buff);
  434. return err;
  435. }
  436. block_dev_desc_t *mg_disk_get_dev(int dev)
  437. {
  438. return ((block_dev_desc_t *) & mg_disk_dev);
  439. }
  440. /* must override this function */
  441. struct mg_drv_data * __attribute__((weak)) mg_get_drv_data (void)
  442. {
  443. puts ("### WARNING ### port mg_get_drv_data function\n");
  444. return NULL;
  445. }
  446. unsigned int mg_disk_init (void)
  447. {
  448. struct mg_drv_data *prv_data;
  449. u32 err = MG_ERR_NONE;
  450. prv_data = mg_get_drv_data();
  451. if (! prv_data) {
  452. printf("%s:%d fail (no driver_data)\n", __func__, __LINE__);
  453. err = MG_ERR_NO_DRV_DATA;
  454. return err;
  455. }
  456. ((struct mg_host *)mg_disk_dev.priv)->drv_data = prv_data;
  457. /* init ctrl pin */
  458. if (prv_data->mg_ctrl_pin_init)
  459. prv_data->mg_ctrl_pin_init();
  460. if (! prv_data->mg_hdrst_pin) {
  461. err = MG_ERR_CTRL_RST;
  462. return err;
  463. }
  464. /* disk reset */
  465. err = mg_disk_reset();
  466. if (err) {
  467. printf("%s:%d fail (err code : %d)\n", __func__, __LINE__, err);
  468. return err;
  469. }
  470. /* get disk id */
  471. err = mg_get_disk_id();
  472. if (err) {
  473. printf("%s:%d fail (err code : %d)\n", __func__, __LINE__, err);
  474. return err;
  475. }
  476. mg_disk_dev.block_read = mg_block_read;
  477. mg_disk_dev.block_write = mg_block_write;
  478. init_part(&mg_disk_dev);
  479. dev_print(&mg_disk_dev);
  480. return err;
  481. }