You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1334 lines
35 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007-2008 by unsik Kim <donari75@gmail.com> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "mflash.h"
  23. #include "time_support.h"
  24. #include "fileio.h"
  25. #include "log.h"
  26. static int s3c2440_set_gpio_to_output (mflash_gpio_num_t gpio);
  27. static int s3c2440_set_gpio_output_val (mflash_gpio_num_t gpio, uint8_t val);
  28. static int pxa270_set_gpio_to_output (mflash_gpio_num_t gpio);
  29. static int pxa270_set_gpio_output_val (mflash_gpio_num_t gpio, uint8_t val);
  30. static command_t *mflash_cmd;
  31. static mflash_bank_t *mflash_bank;
  32. static mflash_gpio_drv_t pxa270_gpio = {
  33. .name = "pxa270",
  34. .set_gpio_to_output = pxa270_set_gpio_to_output,
  35. .set_gpio_output_val = pxa270_set_gpio_output_val
  36. };
  37. static mflash_gpio_drv_t s3c2440_gpio = {
  38. .name = "s3c2440",
  39. .set_gpio_to_output = s3c2440_set_gpio_to_output,
  40. .set_gpio_output_val = s3c2440_set_gpio_output_val
  41. };
  42. static mflash_gpio_drv_t *mflash_gpio[] =
  43. {
  44. &pxa270_gpio,
  45. &s3c2440_gpio,
  46. NULL
  47. };
  48. #define PXA270_GAFR0_L 0x40E00054
  49. #define PXA270_GAFR3_U 0x40E00070
  50. #define PXA270_GAFR3_U_RESERVED_BITS 0xfffc0000u
  51. #define PXA270_GPDR0 0x40E0000C
  52. #define PXA270_GPDR3 0x40E0010C
  53. #define PXA270_GPDR3_RESERVED_BITS 0xfe000000u
  54. #define PXA270_GPSR0 0x40E00018
  55. #define PXA270_GPCR0 0x40E00024
  56. static int pxa270_set_gpio_to_output (mflash_gpio_num_t gpio)
  57. {
  58. uint32_t addr, value, mask;
  59. target_t *target = mflash_bank->target;
  60. int ret;
  61. /* remove alternate function. */
  62. mask = 0x3u << (gpio.num & 0xF)*2;
  63. addr = PXA270_GAFR0_L + (gpio.num >> 4) * 4;
  64. if ((ret = target_read_u32(target, addr, &value)) != ERROR_OK)
  65. return ret;
  66. value &= ~mask;
  67. if (addr == PXA270_GAFR3_U)
  68. value &= ~PXA270_GAFR3_U_RESERVED_BITS;
  69. if ((ret = target_write_u32(target, addr, value)) != ERROR_OK)
  70. return ret;
  71. /* set direction to output */
  72. mask = 0x1u << (gpio.num & 0x1F);
  73. addr = PXA270_GPDR0 + (gpio.num >> 5) * 4;
  74. if ((ret = target_read_u32(target, addr, &value)) != ERROR_OK)
  75. return ret;
  76. value |= mask;
  77. if (addr == PXA270_GPDR3)
  78. value &= ~PXA270_GPDR3_RESERVED_BITS;
  79. ret = target_write_u32(target, addr, value);
  80. return ret;
  81. }
  82. static int pxa270_set_gpio_output_val (mflash_gpio_num_t gpio, uint8_t val)
  83. {
  84. uint32_t addr, value, mask;
  85. target_t *target = mflash_bank->target;
  86. int ret;
  87. mask = 0x1u << (gpio.num & 0x1F);
  88. if (val) {
  89. addr = PXA270_GPSR0 + (gpio.num >> 5) * 4;
  90. } else {
  91. addr = PXA270_GPCR0 + (gpio.num >> 5) * 4;
  92. }
  93. if ((ret = target_read_u32(target, addr, &value)) != ERROR_OK)
  94. return ret;
  95. value |= mask;
  96. ret = target_write_u32(target, addr, value);
  97. return ret;
  98. }
  99. #define S3C2440_GPACON 0x56000000
  100. #define S3C2440_GPADAT 0x56000004
  101. #define S3C2440_GPJCON 0x560000d0
  102. #define S3C2440_GPJDAT 0x560000d4
  103. static int s3c2440_set_gpio_to_output (mflash_gpio_num_t gpio)
  104. {
  105. uint32_t data, mask, gpio_con;
  106. target_t *target = mflash_bank->target;
  107. int ret;
  108. if (gpio.port[0] >= 'a' && gpio.port[0] <= 'h') {
  109. gpio_con = S3C2440_GPACON + (gpio.port[0] - 'a') * 0x10;
  110. } else if (gpio.port[0] == 'j') {
  111. gpio_con = S3C2440_GPJCON;
  112. } else {
  113. LOG_ERROR("mflash: invalid port %d%s", gpio.num, gpio.port);
  114. return ERROR_INVALID_ARGUMENTS;
  115. }
  116. ret = target_read_u32(target, gpio_con, &data);
  117. if (ret == ERROR_OK) {
  118. if (gpio.port[0] == 'a') {
  119. mask = 1 << gpio.num;
  120. data &= ~mask;
  121. } else {
  122. mask = 3 << gpio.num * 2;
  123. data &= ~mask;
  124. data |= (1 << gpio.num * 2);
  125. }
  126. ret = target_write_u32(target, gpio_con, data);
  127. }
  128. return ret;
  129. }
  130. static int s3c2440_set_gpio_output_val (mflash_gpio_num_t gpio, uint8_t val)
  131. {
  132. uint32_t data, mask, gpio_dat;
  133. target_t *target = mflash_bank->target;
  134. int ret;
  135. if (gpio.port[0] >= 'a' && gpio.port[0] <= 'h') {
  136. gpio_dat = S3C2440_GPADAT + (gpio.port[0] - 'a') * 0x10;
  137. } else if (gpio.port[0] == 'j') {
  138. gpio_dat = S3C2440_GPJDAT;
  139. } else {
  140. LOG_ERROR("mflash: invalid port %d%s", gpio.num, gpio.port);
  141. return ERROR_INVALID_ARGUMENTS;
  142. }
  143. ret = target_read_u32(target, gpio_dat, &data);
  144. if (ret == ERROR_OK) {
  145. mask = 1 << gpio.num;
  146. if (val)
  147. data |= mask;
  148. else
  149. data &= ~mask;
  150. ret = target_write_u32(target, gpio_dat, data);
  151. }
  152. return ret;
  153. }
  154. static int mg_hdrst(uint8_t level)
  155. {
  156. return mflash_bank->gpio_drv->set_gpio_output_val(mflash_bank->rst_pin, level);
  157. }
  158. static int mg_init_gpio (void)
  159. {
  160. int ret;
  161. mflash_gpio_drv_t *gpio_drv = mflash_bank->gpio_drv;
  162. ret = gpio_drv->set_gpio_to_output(mflash_bank->rst_pin);
  163. if (ret != ERROR_OK)
  164. return ret;
  165. ret = gpio_drv->set_gpio_output_val(mflash_bank->rst_pin, 1);
  166. return ret;
  167. }
  168. static int mg_dsk_wait(mg_io_type_wait wait, uint32_t time)
  169. {
  170. uint8_t status, error;
  171. target_t *target = mflash_bank->target;
  172. uint32_t mg_task_reg = mflash_bank->base + MG_REG_OFFSET;
  173. duration_t duration;
  174. int ret;
  175. long long t = 0;
  176. duration_start_measure(&duration);
  177. while (time) {
  178. ret = target_read_u8(target, mg_task_reg + MG_REG_STATUS, &status);
  179. if (ret != ERROR_OK)
  180. return ret;
  181. if (status & mg_io_rbit_status_busy)
  182. {
  183. if (wait == mg_io_wait_bsy)
  184. return ERROR_OK;
  185. } else {
  186. switch (wait)
  187. {
  188. case mg_io_wait_not_bsy:
  189. return ERROR_OK;
  190. case mg_io_wait_rdy_noerr:
  191. if (status & mg_io_rbit_status_ready)
  192. return ERROR_OK;
  193. break;
  194. case mg_io_wait_drq_noerr:
  195. if (status & mg_io_rbit_status_data_req)
  196. return ERROR_OK;
  197. break;
  198. default:
  199. break;
  200. }
  201. /* Now we check the error condition! */
  202. if (status & mg_io_rbit_status_error)
  203. {
  204. ret = target_read_u8(target, mg_task_reg + MG_REG_ERROR, &error);
  205. if (ret != ERROR_OK)
  206. return ret;
  207. LOG_ERROR("mflash: io error 0x%02x", error);
  208. return ERROR_MG_IO;
  209. }
  210. switch (wait)
  211. {
  212. case mg_io_wait_rdy:
  213. if (status & mg_io_rbit_status_ready)
  214. return ERROR_OK;
  215. case mg_io_wait_drq:
  216. if (status & mg_io_rbit_status_data_req)
  217. return ERROR_OK;
  218. default:
  219. break;
  220. }
  221. }
  222. duration_stop_measure(&duration, NULL);
  223. t = duration.duration.tv_usec/1000;
  224. t += duration.duration.tv_sec*1000;
  225. if (t > time)
  226. break;
  227. }
  228. LOG_ERROR("mflash: timeout occured");
  229. return ERROR_MG_TIMEOUT;
  230. }
  231. static int mg_dsk_srst(uint8_t on)
  232. {
  233. target_t *target = mflash_bank->target;
  234. uint32_t mg_task_reg = mflash_bank->base + MG_REG_OFFSET;
  235. uint8_t value;
  236. int ret;
  237. if ((ret = target_read_u8(target, mg_task_reg + MG_REG_DRV_CTRL, &value)) != ERROR_OK)
  238. return ret;
  239. if (on) {
  240. value |= (mg_io_rbit_devc_srst);
  241. } else {
  242. value &= ~mg_io_rbit_devc_srst;
  243. }
  244. ret = target_write_u8(target, mg_task_reg + MG_REG_DRV_CTRL, value);
  245. return ret;
  246. }
  247. static int mg_dsk_io_cmd(uint32_t sect_num, uint32_t cnt, uint8_t cmd)
  248. {
  249. target_t *target = mflash_bank->target;
  250. uint32_t mg_task_reg = mflash_bank->base + MG_REG_OFFSET;
  251. uint8_t value;
  252. int ret;
  253. ret = mg_dsk_wait(mg_io_wait_rdy_noerr, MG_OEM_DISK_WAIT_TIME_NORMAL);
  254. if (ret != ERROR_OK)
  255. return ret;
  256. value = mg_io_rval_dev_drv_master | mg_io_rval_dev_lba_mode |((sect_num >> 24) & 0xf);
  257. ret = target_write_u8(target, mg_task_reg + MG_REG_DRV_HEAD, value);
  258. ret |= target_write_u8(target, mg_task_reg + MG_REG_SECT_CNT, (uint8_t)cnt);
  259. ret |= target_write_u8(target, mg_task_reg + MG_REG_SECT_NUM, (uint8_t)sect_num);
  260. ret |= target_write_u8(target, mg_task_reg + MG_REG_CYL_LOW, (uint8_t)(sect_num >> 8));
  261. ret |= target_write_u8(target, mg_task_reg + MG_REG_CYL_HIGH, (uint8_t)(sect_num >> 16));
  262. if (ret != ERROR_OK)
  263. return ret;
  264. return target_write_u8(target, mg_task_reg + MG_REG_COMMAND, cmd);
  265. }
  266. static int mg_dsk_drv_info(void)
  267. {
  268. target_t *target = mflash_bank->target;
  269. uint32_t mg_buff = mflash_bank->base + MG_BUFFER_OFFSET;
  270. int ret;
  271. if ((ret = mg_dsk_io_cmd(0, 1, mg_io_cmd_identify)) != ERROR_OK)
  272. return ret;
  273. if ((ret = mg_dsk_wait(mg_io_wait_drq, MG_OEM_DISK_WAIT_TIME_NORMAL)) != ERROR_OK)
  274. return ret;
  275. LOG_INFO("mflash: read drive info");
  276. if (! mflash_bank->drv_info)
  277. mflash_bank->drv_info = malloc(sizeof(mg_drv_info_t));
  278. target_read_memory(target, mg_buff, 2, sizeof(mg_io_type_drv_info) >> 1,
  279. (uint8_t *)&mflash_bank->drv_info->drv_id);
  280. if (ret != ERROR_OK)
  281. return ret;
  282. mflash_bank->drv_info->tot_sects = (uint32_t)(mflash_bank->drv_info->drv_id.total_user_addressable_sectors_hi << 16)
  283. + mflash_bank->drv_info->drv_id.total_user_addressable_sectors_lo;
  284. return target_write_u8(target, mflash_bank->base + MG_REG_OFFSET + MG_REG_COMMAND, mg_io_cmd_confirm_read);
  285. }
  286. static int mg_mflash_rst(void)
  287. {
  288. int ret;
  289. if ((ret = mg_init_gpio()) != ERROR_OK)
  290. return ret;
  291. if ((ret = mg_hdrst(0)) != ERROR_OK)
  292. return ret;
  293. if ((ret = mg_dsk_wait(mg_io_wait_bsy, MG_OEM_DISK_WAIT_TIME_LONG)) != ERROR_OK)
  294. return ret;
  295. if ((ret = mg_hdrst(1)) != ERROR_OK)
  296. return ret;
  297. if ((ret = mg_dsk_wait(mg_io_wait_not_bsy, MG_OEM_DISK_WAIT_TIME_LONG)) != ERROR_OK)
  298. return ret;
  299. if ((ret = mg_dsk_srst(1)) != ERROR_OK)
  300. return ret;
  301. if ((ret = mg_dsk_wait(mg_io_wait_bsy, MG_OEM_DISK_WAIT_TIME_LONG)) != ERROR_OK)
  302. return ret;
  303. if ((ret = mg_dsk_srst(0)) != ERROR_OK)
  304. return ret;
  305. if ((ret = mg_dsk_wait(mg_io_wait_not_bsy, MG_OEM_DISK_WAIT_TIME_LONG)) != ERROR_OK)
  306. return ret;
  307. LOG_INFO("mflash: reset ok");
  308. return ERROR_OK;
  309. }
  310. static int mg_mflash_probe(void)
  311. {
  312. int ret;
  313. if ((ret = mg_mflash_rst()) != ERROR_OK)
  314. return ret;
  315. return mg_dsk_drv_info();
  316. }
  317. static int mg_probe_cmd(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  318. {
  319. int ret;
  320. ret = mg_mflash_probe();
  321. if (ret == ERROR_OK) {
  322. command_print(cmd_ctx, "mflash (total %" PRIu32 " sectors) found at 0x%8.8" PRIx32 "",
  323. mflash_bank->drv_info->tot_sects, mflash_bank->base);
  324. }
  325. return ret;
  326. }
  327. static int mg_mflash_do_read_sects(void *buff, uint32_t sect_num, uint32_t sect_cnt)
  328. {
  329. uint32_t i, address;
  330. int ret;
  331. target_t *target = mflash_bank->target;
  332. uint8_t *buff_ptr = buff;
  333. duration_t duration;
  334. if ((ret = mg_dsk_io_cmd(sect_num, sect_cnt, mg_io_cmd_read)) != ERROR_OK)
  335. return ret;
  336. address = mflash_bank->base + MG_BUFFER_OFFSET;
  337. duration_start_measure(&duration);
  338. for (i = 0; i < sect_cnt; i++) {
  339. ret = mg_dsk_wait(mg_io_wait_drq, MG_OEM_DISK_WAIT_TIME_NORMAL);
  340. if (ret != ERROR_OK)
  341. return ret;
  342. ret = target_read_memory(target, address, 2, MG_MFLASH_SECTOR_SIZE / 2, buff_ptr);
  343. if (ret != ERROR_OK)
  344. return ret;
  345. buff_ptr += MG_MFLASH_SECTOR_SIZE;
  346. ret = target_write_u8(target, mflash_bank->base + MG_REG_OFFSET + MG_REG_COMMAND, mg_io_cmd_confirm_read);
  347. if (ret != ERROR_OK)
  348. return ret;
  349. LOG_DEBUG("mflash: %" PRIu32 " (0x%8.8" PRIx32 ") sector read", sect_num + i, (sect_num + i) * MG_MFLASH_SECTOR_SIZE);
  350. duration_stop_measure(&duration, NULL);
  351. if ((duration.duration.tv_sec * 1000 + duration.duration.tv_usec / 1000) > 3000) {
  352. LOG_INFO("mflash: read %" PRIu32 "'th sectors", sect_num + i);
  353. duration_start_measure(&duration);
  354. }
  355. }
  356. return mg_dsk_wait(mg_io_wait_rdy, MG_OEM_DISK_WAIT_TIME_NORMAL);
  357. }
  358. static int mg_mflash_read_sects(void *buff, uint32_t sect_num, uint32_t sect_cnt)
  359. {
  360. uint32_t quotient, residue, i;
  361. uint8_t *buff_ptr = buff;
  362. int ret = ERROR_OK;
  363. quotient = sect_cnt >> 8;
  364. residue = sect_cnt % 256;
  365. for (i = 0; i < quotient; i++) {
  366. LOG_DEBUG("mflash: sect num : %" PRIu32 " buff : 0x%0lx", sect_num,
  367. (unsigned long)buff_ptr);
  368. ret = mg_mflash_do_read_sects(buff_ptr, sect_num, 256);
  369. if (ret != ERROR_OK)
  370. return ret;
  371. sect_num += 256;
  372. buff_ptr += 256 * MG_MFLASH_SECTOR_SIZE;
  373. }
  374. if (residue) {
  375. LOG_DEBUG("mflash: sect num : %" PRIx32 " buff : %0lx", sect_num,
  376. (unsigned long)buff_ptr);
  377. return mg_mflash_do_read_sects(buff_ptr, sect_num, residue);
  378. }
  379. return ret;
  380. }
  381. static int mg_mflash_do_write_sects(void *buff, uint32_t sect_num, uint32_t sect_cnt,
  382. mg_io_type_cmd cmd)
  383. {
  384. uint32_t i, address;
  385. int ret;
  386. target_t *target = mflash_bank->target;
  387. uint8_t *buff_ptr = buff;
  388. duration_t duration;
  389. if ((ret = mg_dsk_io_cmd(sect_num, sect_cnt, cmd)) != ERROR_OK)
  390. return ret;
  391. address = mflash_bank->base + MG_BUFFER_OFFSET;
  392. duration_start_measure(&duration);
  393. for (i = 0; i < sect_cnt; i++) {
  394. ret = mg_dsk_wait(mg_io_wait_drq, MG_OEM_DISK_WAIT_TIME_NORMAL);
  395. if (ret != ERROR_OK)
  396. return ret;
  397. ret = target_write_memory(target, address, 2, MG_MFLASH_SECTOR_SIZE / 2, buff_ptr);
  398. if (ret != ERROR_OK)
  399. return ret;
  400. buff_ptr += MG_MFLASH_SECTOR_SIZE;
  401. ret = target_write_u8(target, mflash_bank->base + MG_REG_OFFSET + MG_REG_COMMAND, mg_io_cmd_confirm_write);
  402. if (ret != ERROR_OK)
  403. return ret;
  404. LOG_DEBUG("mflash: %" PRIu32 " (0x%8.8" PRIx32 ") sector write", sect_num + i, (sect_num + i) * MG_MFLASH_SECTOR_SIZE);
  405. duration_stop_measure(&duration, NULL);
  406. if ((duration.duration.tv_sec * 1000 + duration.duration.tv_usec / 1000) > 3000) {
  407. LOG_INFO("mflash: wrote %" PRIu32 "'th sectors", sect_num + i);
  408. duration_start_measure(&duration);
  409. }
  410. }
  411. if (cmd == mg_io_cmd_write)
  412. ret = mg_dsk_wait(mg_io_wait_rdy, MG_OEM_DISK_WAIT_TIME_NORMAL);
  413. else
  414. ret = mg_dsk_wait(mg_io_wait_rdy, MG_OEM_DISK_WAIT_TIME_LONG);
  415. return ret;
  416. }
  417. static int mg_mflash_write_sects(void *buff, uint32_t sect_num, uint32_t sect_cnt)
  418. {
  419. uint32_t quotient, residue, i;
  420. uint8_t *buff_ptr = buff;
  421. int ret = ERROR_OK;
  422. quotient = sect_cnt >> 8;
  423. residue = sect_cnt % 256;
  424. for (i = 0; i < quotient; i++) {
  425. LOG_DEBUG("mflash: sect num : %" PRIu32 "buff : %p", sect_num,
  426. buff_ptr);
  427. ret = mg_mflash_do_write_sects(buff_ptr, sect_num, 256, mg_io_cmd_write);
  428. if (ret != ERROR_OK)
  429. return ret;
  430. sect_num += 256;
  431. buff_ptr += 256 * MG_MFLASH_SECTOR_SIZE;
  432. }
  433. if (residue) {
  434. LOG_DEBUG("mflash: sect num : %" PRIu32 " buff : %p", sect_num,
  435. buff_ptr);
  436. return mg_mflash_do_write_sects(buff_ptr, sect_num, residue, mg_io_cmd_write);
  437. }
  438. return ret;
  439. }
  440. static int mg_mflash_read (uint32_t addr, uint8_t *buff, uint32_t len)
  441. {
  442. uint8_t *buff_ptr = buff;
  443. uint8_t sect_buff[MG_MFLASH_SECTOR_SIZE];
  444. uint32_t cur_addr, next_sec_addr, end_addr, cnt, sect_num;
  445. int ret = ERROR_OK;
  446. cnt = 0;
  447. cur_addr = addr;
  448. end_addr = addr + len;
  449. if (cur_addr & MG_MFLASH_SECTOR_SIZE_MASK) {
  450. next_sec_addr = (cur_addr + MG_MFLASH_SECTOR_SIZE) & ~MG_MFLASH_SECTOR_SIZE_MASK;
  451. sect_num = cur_addr >> MG_MFLASH_SECTOR_SIZE_SHIFT;
  452. ret = mg_mflash_read_sects(sect_buff, sect_num, 1);
  453. if (ret != ERROR_OK)
  454. return ret;
  455. if (end_addr < next_sec_addr) {
  456. memcpy(buff_ptr, sect_buff + (cur_addr & MG_MFLASH_SECTOR_SIZE_MASK), end_addr - cur_addr);
  457. LOG_DEBUG("mflash: copies %" PRIu32 " byte from sector offset 0x%8.8" PRIx32 "", end_addr - cur_addr, cur_addr);
  458. cur_addr = end_addr;
  459. } else {
  460. memcpy(buff_ptr, sect_buff + (cur_addr & MG_MFLASH_SECTOR_SIZE_MASK), next_sec_addr - cur_addr);
  461. LOG_DEBUG("mflash: copies %" PRIu32 " byte from sector offset 0x%8.8" PRIx32 "", next_sec_addr - cur_addr, cur_addr);
  462. buff_ptr += (next_sec_addr - cur_addr);
  463. cur_addr = next_sec_addr;
  464. }
  465. }
  466. if (cur_addr < end_addr) {
  467. sect_num = cur_addr >> MG_MFLASH_SECTOR_SIZE_SHIFT;
  468. next_sec_addr = cur_addr + MG_MFLASH_SECTOR_SIZE;
  469. while (next_sec_addr <= end_addr) {
  470. cnt++;
  471. next_sec_addr += MG_MFLASH_SECTOR_SIZE;
  472. }
  473. if (cnt)
  474. if ((ret = mg_mflash_read_sects(buff_ptr, sect_num, cnt)) != ERROR_OK)
  475. return ret;
  476. buff_ptr += cnt * MG_MFLASH_SECTOR_SIZE;
  477. cur_addr += cnt * MG_MFLASH_SECTOR_SIZE;
  478. if (cur_addr < end_addr) {
  479. sect_num = cur_addr >> MG_MFLASH_SECTOR_SIZE_SHIFT;
  480. ret = mg_mflash_read_sects(sect_buff, sect_num, 1);
  481. if (ret != ERROR_OK)
  482. return ret;
  483. memcpy(buff_ptr, sect_buff, end_addr - cur_addr);
  484. LOG_DEBUG("mflash: copies %u byte", (unsigned)(end_addr - cur_addr));
  485. }
  486. }
  487. return ret;
  488. }
  489. static int mg_mflash_write(uint32_t addr, uint8_t *buff, uint32_t len)
  490. {
  491. uint8_t *buff_ptr = buff;
  492. uint8_t sect_buff[MG_MFLASH_SECTOR_SIZE];
  493. uint32_t cur_addr, next_sec_addr, end_addr, cnt, sect_num;
  494. int ret = ERROR_OK;
  495. cnt = 0;
  496. cur_addr = addr;
  497. end_addr = addr + len;
  498. if (cur_addr & MG_MFLASH_SECTOR_SIZE_MASK) {
  499. next_sec_addr = (cur_addr + MG_MFLASH_SECTOR_SIZE) & ~MG_MFLASH_SECTOR_SIZE_MASK;
  500. sect_num = cur_addr >> MG_MFLASH_SECTOR_SIZE_SHIFT;
  501. ret = mg_mflash_read_sects(sect_buff, sect_num, 1);
  502. if (ret != ERROR_OK)
  503. return ret;
  504. if (end_addr < next_sec_addr) {
  505. memcpy(sect_buff + (cur_addr & MG_MFLASH_SECTOR_SIZE_MASK), buff_ptr, end_addr - cur_addr);
  506. LOG_DEBUG("mflash: copies %" PRIu32 " byte to sector offset 0x%8.8" PRIx32 "", end_addr - cur_addr, cur_addr);
  507. cur_addr = end_addr;
  508. } else {
  509. memcpy(sect_buff + (cur_addr & MG_MFLASH_SECTOR_SIZE_MASK), buff_ptr, next_sec_addr - cur_addr);
  510. LOG_DEBUG("mflash: copies %" PRIu32 " byte to sector offset 0x%8.8" PRIx32 "", next_sec_addr - cur_addr, cur_addr);
  511. buff_ptr += (next_sec_addr - cur_addr);
  512. cur_addr = next_sec_addr;
  513. }
  514. ret = mg_mflash_write_sects(sect_buff, sect_num, 1);
  515. if (ret != ERROR_OK)
  516. return ret;
  517. }
  518. if (cur_addr < end_addr) {
  519. sect_num = cur_addr >> MG_MFLASH_SECTOR_SIZE_SHIFT;
  520. next_sec_addr = cur_addr + MG_MFLASH_SECTOR_SIZE;
  521. while (next_sec_addr <= end_addr) {
  522. cnt++;
  523. next_sec_addr += MG_MFLASH_SECTOR_SIZE;
  524. }
  525. if (cnt)
  526. if ((ret = mg_mflash_write_sects(buff_ptr, sect_num, cnt)) != ERROR_OK)
  527. return ret;
  528. buff_ptr += cnt * MG_MFLASH_SECTOR_SIZE;
  529. cur_addr += cnt * MG_MFLASH_SECTOR_SIZE;
  530. if (cur_addr < end_addr) {
  531. sect_num = cur_addr >> MG_MFLASH_SECTOR_SIZE_SHIFT;
  532. ret = mg_mflash_read_sects(sect_buff, sect_num, 1);
  533. if (ret != ERROR_OK)
  534. return ret;
  535. memcpy(sect_buff, buff_ptr, end_addr - cur_addr);
  536. LOG_DEBUG("mflash: copies %" PRIu32 " byte", end_addr - cur_addr);
  537. ret = mg_mflash_write_sects(sect_buff, sect_num, 1);
  538. }
  539. }
  540. return ret;
  541. }
  542. static int mg_write_cmd(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  543. {
  544. uint32_t address, buf_cnt, cnt, res, i;
  545. uint8_t *buffer;
  546. fileio_t fileio;
  547. duration_t duration;
  548. char *duration_text;
  549. int ret;
  550. if (argc != 3) {
  551. return ERROR_COMMAND_SYNTAX_ERROR;
  552. }
  553. address = strtoul(args[2], NULL, 0);
  554. ret = fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY);
  555. if (ret != ERROR_OK)
  556. return ret;
  557. buffer = malloc(MG_FILEIO_CHUNK);
  558. if (!buffer) {
  559. fileio_close(&fileio);
  560. return ERROR_FAIL;
  561. }
  562. cnt = fileio.size / MG_FILEIO_CHUNK;
  563. res = fileio.size % MG_FILEIO_CHUNK;
  564. duration_start_measure(&duration);
  565. for (i = 0; i < cnt; i++) {
  566. if ((ret = fileio_read(&fileio, MG_FILEIO_CHUNK, buffer, &buf_cnt)) !=
  567. ERROR_OK)
  568. goto mg_write_cmd_err;
  569. if ((ret = mg_mflash_write(address, buffer, MG_FILEIO_CHUNK)) != ERROR_OK)
  570. goto mg_write_cmd_err;
  571. address += MG_FILEIO_CHUNK;
  572. }
  573. if (res) {
  574. if ((ret = fileio_read(&fileio, res, buffer, &buf_cnt)) != ERROR_OK)
  575. goto mg_write_cmd_err;
  576. if ((ret = mg_mflash_write(address, buffer, res)) != ERROR_OK)
  577. goto mg_write_cmd_err;
  578. }
  579. duration_stop_measure(&duration, &duration_text);
  580. command_print(cmd_ctx, "wrote %lli byte from file %s in %s (%f kB/s)",
  581. fileio.size, args[1], duration_text,
  582. (float)fileio.size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
  583. free(duration_text);
  584. free(buffer);
  585. fileio_close(&fileio);
  586. return ERROR_OK;
  587. mg_write_cmd_err:
  588. duration_stop_measure(&duration, &duration_text);
  589. free(duration_text);
  590. free(buffer);
  591. fileio_close(&fileio);
  592. return ret;
  593. }
  594. static int mg_dump_cmd(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  595. {
  596. uint32_t address, size_written, size, cnt, res, i;
  597. uint8_t *buffer;
  598. fileio_t fileio;
  599. duration_t duration;
  600. char *duration_text;
  601. int ret;
  602. if (argc != 4) {
  603. return ERROR_COMMAND_SYNTAX_ERROR;
  604. }
  605. address = strtoul(args[2], NULL, 0);
  606. size = strtoul(args[3], NULL, 0);
  607. ret = fileio_open(&fileio, args[1], FILEIO_WRITE, FILEIO_BINARY);
  608. if (ret != ERROR_OK)
  609. return ret;
  610. buffer = malloc(MG_FILEIO_CHUNK);
  611. if (!buffer) {
  612. fileio_close(&fileio);
  613. return ERROR_FAIL;
  614. }
  615. cnt = size / MG_FILEIO_CHUNK;
  616. res = size % MG_FILEIO_CHUNK;
  617. duration_start_measure(&duration);
  618. for (i = 0; i < cnt; i++) {
  619. if ((ret = mg_mflash_read(address, buffer, MG_FILEIO_CHUNK)) != ERROR_OK)
  620. goto mg_dump_cmd_err;
  621. if ((ret = fileio_write(&fileio, MG_FILEIO_CHUNK, buffer, &size_written))
  622. != ERROR_OK)
  623. goto mg_dump_cmd_err;
  624. address += MG_FILEIO_CHUNK;
  625. }
  626. if (res) {
  627. if ((ret = mg_mflash_read(address, buffer, res)) != ERROR_OK)
  628. goto mg_dump_cmd_err;
  629. if ((ret = fileio_write(&fileio, res, buffer, &size_written)) != ERROR_OK)
  630. goto mg_dump_cmd_err;
  631. }
  632. duration_stop_measure(&duration, &duration_text);
  633. command_print(cmd_ctx, "dump image (address 0x%8.8" PRIx32 " size %" PRIu32 ") to file %s in %s (%f kB/s)",
  634. address, size, args[1], duration_text,
  635. (float)size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
  636. free(duration_text);
  637. free(buffer);
  638. fileio_close(&fileio);
  639. return ERROR_OK;
  640. mg_dump_cmd_err:
  641. duration_stop_measure(&duration, &duration_text);
  642. free(duration_text);
  643. free(buffer);
  644. fileio_close(&fileio);
  645. return ret;
  646. }
  647. static int mg_set_feature(mg_feature_id feature, mg_feature_val config)
  648. {
  649. target_t *target = mflash_bank->target;
  650. uint32_t mg_task_reg = mflash_bank->base + MG_REG_OFFSET;
  651. int ret;
  652. if ((ret = mg_dsk_wait(mg_io_wait_rdy_noerr, MG_OEM_DISK_WAIT_TIME_NORMAL))
  653. != ERROR_OK)
  654. return ret;
  655. ret = target_write_u8(target, mg_task_reg + MG_REG_FEATURE, feature);
  656. ret |= target_write_u8(target, mg_task_reg + MG_REG_SECT_CNT, config);
  657. ret |= target_write_u8(target, mg_task_reg + MG_REG_COMMAND,
  658. mg_io_cmd_set_feature);
  659. return ret;
  660. }
  661. static int mg_is_valid_pll(double XIN, int N, double CLK_OUT, int NO)
  662. {
  663. double v1 = XIN / N;
  664. double v2 = CLK_OUT * NO;
  665. if (v1 <1000000 || v1 > 15000000 || v2 < 100000000 || v2 > 500000000)
  666. return ERROR_MG_INVALID_PLL;
  667. return ERROR_OK;
  668. }
  669. static int mg_pll_get_M(unsigned short feedback_div)
  670. {
  671. int i, M;
  672. for (i = 1, M = 0; i < 512; i <<= 1, feedback_div >>= 1)
  673. M += (feedback_div & 1) * i;
  674. return M + 2;
  675. }
  676. static int mg_pll_get_N(unsigned char input_div)
  677. {
  678. int i, N;
  679. for (i = 1, N = 0; i < 32; i <<= 1, input_div >>= 1)
  680. N += (input_div & 1) * i;
  681. return N + 2;
  682. }
  683. static int mg_pll_get_NO(unsigned char output_div)
  684. {
  685. int i, NO;
  686. for (i = 0, NO = 1; i < 2; ++i, output_div >>= 1)
  687. if (output_div & 1)
  688. NO = NO << 1;
  689. return NO;
  690. }
  691. static double mg_do_calc_pll(double XIN, mg_pll_t * p_pll_val, int is_approximate)
  692. {
  693. unsigned short i;
  694. unsigned char j, k;
  695. int M, N, NO;
  696. double CLK_OUT;
  697. double DIV = 1;
  698. double ROUND = 0;
  699. if (is_approximate) {
  700. DIV = 1000000;
  701. ROUND = 500000;
  702. }
  703. for (i = 0; i < MG_PLL_MAX_FEEDBACKDIV_VAL ; ++i) {
  704. M = mg_pll_get_M(i);
  705. for (j = 0; j < MG_PLL_MAX_INPUTDIV_VAL ; ++j) {
  706. N = mg_pll_get_N(j);
  707. for (k = 0; k < MG_PLL_MAX_OUTPUTDIV_VAL ; ++k) {
  708. NO = mg_pll_get_NO(k);
  709. CLK_OUT = XIN * ((double)M / N) / NO;
  710. if ((int)((CLK_OUT + ROUND) / DIV)
  711. == (int)(MG_PLL_CLK_OUT / DIV)) {
  712. if (mg_is_valid_pll(XIN, N, CLK_OUT, NO) == ERROR_OK)
  713. {
  714. p_pll_val->lock_cyc = (int)(XIN * MG_PLL_STD_LOCKCYCLE / MG_PLL_STD_INPUTCLK);
  715. p_pll_val->feedback_div = i;
  716. p_pll_val->input_div = j;
  717. p_pll_val->output_div = k;
  718. return CLK_OUT;
  719. }
  720. }
  721. }
  722. }
  723. }
  724. return 0;
  725. }
  726. static double mg_calc_pll(double XIN, mg_pll_t *p_pll_val)
  727. {
  728. double CLK_OUT;
  729. CLK_OUT = mg_do_calc_pll(XIN, p_pll_val, 0);
  730. if (!CLK_OUT)
  731. return mg_do_calc_pll(XIN, p_pll_val, 1);
  732. else
  733. return CLK_OUT;
  734. }
  735. static int mg_verify_interface(void)
  736. {
  737. uint16_t buff[MG_MFLASH_SECTOR_SIZE >> 1];
  738. uint16_t i, j;
  739. uint32_t address = mflash_bank->base + MG_BUFFER_OFFSET;
  740. target_t *target = mflash_bank->target;
  741. int ret;
  742. for (j = 0; j < 10; j++) {
  743. for (i = 0; i < MG_MFLASH_SECTOR_SIZE >> 1; i++)
  744. buff[i] = i;
  745. ret = target_write_memory(target, address, 2,
  746. MG_MFLASH_SECTOR_SIZE / 2, (uint8_t *)buff);
  747. if (ret != ERROR_OK)
  748. return ret;
  749. memset(buff, 0xff, MG_MFLASH_SECTOR_SIZE);
  750. ret = target_read_memory(target, address, 2,
  751. MG_MFLASH_SECTOR_SIZE / 2, (uint8_t *)buff);
  752. if (ret != ERROR_OK)
  753. return ret;
  754. for (i = 0; i < MG_MFLASH_SECTOR_SIZE >> 1; i++) {
  755. if (buff[i] != i) {
  756. LOG_ERROR("mflash: verify interface fail");
  757. return ERROR_MG_INTERFACE;
  758. }
  759. }
  760. }
  761. LOG_INFO("mflash: verify interface ok");
  762. return ret;
  763. }
  764. static const char g_strSEG_SerialNum[20] = {
  765. 'G','m','n','i','-','e','e','S','g','a','e','l',
  766. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
  767. };
  768. static const char g_strSEG_FWRev[8] = {
  769. 'F','X','L','T','2','v','0','.'
  770. };
  771. static const char g_strSEG_ModelNum[40] = {
  772. 'F','X','A','L','H','S','2',0x20,'0','0','s','7',
  773. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  774. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
  775. 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20
  776. };
  777. static void mg_gen_ataid(mg_io_type_drv_info *pSegIdDrvInfo)
  778. {
  779. /* b15 is ATA device(0) , b7 is Removable Media Device */
  780. pSegIdDrvInfo->general_configuration = 0x045A;
  781. /* 128MB : Cylinder=> 977 , Heads=> 8 , Sectors=> 32
  782. * 256MB : Cylinder=> 980 , Heads=> 16 , Sectors=> 32
  783. * 384MB : Cylinder=> 745 , Heads=> 16 , Sectors=> 63
  784. */
  785. pSegIdDrvInfo->number_of_cylinders = 0x02E9;
  786. pSegIdDrvInfo->reserved1 = 0x0;
  787. pSegIdDrvInfo->number_of_heads = 0x10;
  788. pSegIdDrvInfo->unformatted_bytes_per_track = 0x0;
  789. pSegIdDrvInfo->unformatted_bytes_per_sector = 0x0;
  790. pSegIdDrvInfo->sectors_per_track = 0x3F;
  791. pSegIdDrvInfo->vendor_unique1[0] = 0x000B;
  792. pSegIdDrvInfo->vendor_unique1[1] = 0x7570;
  793. pSegIdDrvInfo->vendor_unique1[2] = 0x8888;
  794. memcpy(pSegIdDrvInfo->serial_number, (void *)g_strSEG_SerialNum,20);
  795. /* 0x2 : dual buffer */
  796. pSegIdDrvInfo->buffer_type = 0x2;
  797. /* buffer size : 2KB */
  798. pSegIdDrvInfo->buffer_sector_size = 0x800;
  799. pSegIdDrvInfo->number_of_ecc_bytes = 0;
  800. memcpy(pSegIdDrvInfo->firmware_revision, (void *)g_strSEG_FWRev,8);
  801. memcpy(pSegIdDrvInfo->model_number, (void *)g_strSEG_ModelNum,40);
  802. pSegIdDrvInfo->maximum_block_transfer = 0x4;
  803. pSegIdDrvInfo->vendor_unique2 = 0x0;
  804. pSegIdDrvInfo->dword_io = 0x00;
  805. /* b11 : IORDY support(PIO Mode 4), b10 : Disable/Enbale IORDY
  806. * b9 : LBA support, b8 : DMA mode support
  807. */
  808. pSegIdDrvInfo->capabilities = 0x1 << 9;
  809. pSegIdDrvInfo->reserved2 = 0x4000;
  810. pSegIdDrvInfo->vendor_unique3 = 0x00;
  811. /* PIOMode-2 support */
  812. pSegIdDrvInfo->pio_cycle_timing_mode = 0x02;
  813. pSegIdDrvInfo->vendor_unique4 = 0x00;
  814. /* MultiWord-2 support */
  815. pSegIdDrvInfo->dma_cycle_timing_mode = 0x00;
  816. /* b1 : word64~70 is valid
  817. * b0 : word54~58 are valid and reflect the current numofcyls,heads,sectors
  818. * b2 : If device supports Ultra DMA , set to one to vaildate word88
  819. */
  820. pSegIdDrvInfo->translation_fields_valid = (0x1 << 1) | (0x1 << 0);
  821. pSegIdDrvInfo->number_of_current_cylinders = 0x02E9;
  822. pSegIdDrvInfo->number_of_current_heads = 0x10;
  823. pSegIdDrvInfo->current_sectors_per_track = 0x3F;
  824. pSegIdDrvInfo->current_sector_capacity_lo = 0x7570;
  825. pSegIdDrvInfo->current_sector_capacity_hi = 0x000B;
  826. pSegIdDrvInfo->multi_sector_count = 0x04;
  827. /* b8 : Multiple secotr setting valid , b[7:0] num of secotrs per block */
  828. pSegIdDrvInfo->multi_sector_setting_valid = 0x01;
  829. pSegIdDrvInfo->total_user_addressable_sectors_lo = 0x7570;
  830. pSegIdDrvInfo->total_user_addressable_sectors_hi = 0x000B;
  831. pSegIdDrvInfo->single_dma_modes_supported = 0x00;
  832. pSegIdDrvInfo->single_dma_transfer_active = 0x00;
  833. /* b2 :Multi-word DMA mode 2, b1 : Multi-word DMA mode 1 */
  834. pSegIdDrvInfo->multi_dma_modes_supported = (0x1 << 0);
  835. /* b2 :Multi-word DMA mode 2, b1 : Multi-word DMA mode 1 */
  836. pSegIdDrvInfo->multi_dma_transfer_active = (0x1 << 0);
  837. /* b0 : PIO Mode-3 support, b1 : PIO Mode-4 support */
  838. pSegIdDrvInfo->adv_pio_mode = 0x00;
  839. /* 480(0x1E0)nsec for Multi-word DMA mode0
  840. * 150(0x96) nsec for Multi-word DMA mode1
  841. * 120(0x78) nsec for Multi-word DMA mode2
  842. */
  843. pSegIdDrvInfo->min_dma_cyc = 0x1E0;
  844. pSegIdDrvInfo->recommend_dma_cyc = 0x1E0;
  845. pSegIdDrvInfo->min_pio_cyc_no_iordy = 0x1E0;
  846. pSegIdDrvInfo->min_pio_cyc_with_iordy = 0x1E0;
  847. memset((void *)pSegIdDrvInfo->reserved3, 0x00, 22);
  848. /* b7 : ATA/ATAPI-7 ,b6 : ATA/ATAPI-6 ,b5 : ATA/ATAPI-5,b4 : ATA/ATAPI-4 */
  849. pSegIdDrvInfo->major_ver_num = 0x7E;
  850. /* 0x1C : ATA/ATAPI-6 T13 1532D revision1 */
  851. pSegIdDrvInfo->minor_ver_num = 0x19;
  852. /* NOP/READ BUFFER/WRITE BUFFER/Power management feature set support */
  853. pSegIdDrvInfo->feature_cmd_set_suprt0 = 0x7068;
  854. /* Features/command set is valid/Advanced Pwr management/CFA feature set
  855. * not support
  856. */
  857. pSegIdDrvInfo->feature_cmd_set_suprt1 = 0x400C;
  858. pSegIdDrvInfo->feature_cmd_set_suprt2 = 0x4000;
  859. /* READ/WRITE BUFFER/PWR Management enable */
  860. pSegIdDrvInfo->feature_cmd_set_en0 = 0x7000;
  861. /* CFA feature is disabled / Advancde power management disable */
  862. pSegIdDrvInfo->feature_cmd_set_en1 = 0x0;
  863. pSegIdDrvInfo->feature_cmd_set_en2 = 0x4000;
  864. pSegIdDrvInfo->reserved4 = 0x0;
  865. /* 0x1 * 2minutes */
  866. pSegIdDrvInfo->req_time_for_security_er_done = 0x19;
  867. pSegIdDrvInfo->req_time_for_enhan_security_er_done = 0x19;
  868. /* Advanced power management level 1 */
  869. pSegIdDrvInfo->adv_pwr_mgm_lvl_val = 0x0;
  870. pSegIdDrvInfo->reserved5 = 0x0;
  871. memset((void *)pSegIdDrvInfo->reserved6, 0x00, 68);
  872. /* Security mode feature is disabled */
  873. pSegIdDrvInfo->security_stas = 0x0;
  874. memset((void *)pSegIdDrvInfo->vendor_uniq_bytes, 0x00, 62);
  875. /* CFA power mode 1 support in maximum 200mA */
  876. pSegIdDrvInfo->cfa_pwr_mode = 0x0100;
  877. memset((void *)pSegIdDrvInfo->reserved7, 0x00, 190);
  878. }
  879. static int mg_storage_config(void)
  880. {
  881. uint8_t buff[512];
  882. int ret;
  883. if ((ret = mg_set_feature(mg_feature_id_transmode, mg_feature_val_trans_vcmd))
  884. != ERROR_OK)
  885. return ret;
  886. mg_gen_ataid((mg_io_type_drv_info *)buff);
  887. if ((ret = mg_mflash_do_write_sects(buff, 0, 1, mg_vcmd_update_stgdrvinfo))
  888. != ERROR_OK)
  889. return ret;
  890. if ((ret = mg_set_feature(mg_feature_id_transmode, mg_feature_val_trans_default))
  891. != ERROR_OK)
  892. return ret;
  893. LOG_INFO("mflash: storage config ok");
  894. return ret;
  895. }
  896. static int mg_boot_config(void)
  897. {
  898. uint8_t buff[512];
  899. int ret;
  900. if ((ret = mg_set_feature(mg_feature_id_transmode, mg_feature_val_trans_vcmd))
  901. != ERROR_OK)
  902. return ret;
  903. memset(buff, 0xff, 512);
  904. buff[0] = mg_op_mode_snd; /* operation mode */
  905. buff[1] = MG_UNLOCK_OTP_AREA;
  906. buff[2] = 4; /* boot size */
  907. *((uint32_t *)(buff + 4)) = 0; /* XIP size */
  908. if ((ret = mg_mflash_do_write_sects(buff, 0, 1, mg_vcmd_update_xipinfo))
  909. != ERROR_OK)
  910. return ret;
  911. if ((ret = mg_set_feature(mg_feature_id_transmode, mg_feature_val_trans_default))
  912. != ERROR_OK)
  913. return ret;
  914. LOG_INFO("mflash: boot config ok");
  915. return ret;
  916. }
  917. static int mg_set_pll(mg_pll_t *pll)
  918. {
  919. uint8_t buff[512];
  920. int ret;
  921. memset(buff, 0xff, 512);
  922. /* PLL Lock cycle and Feedback 9bit Divider */
  923. memcpy(buff, &pll->lock_cyc, sizeof(uint32_t));
  924. memcpy(buff + 4, &pll->feedback_div, sizeof(uint16_t));
  925. buff[6] = pll->input_div; /* PLL Input 5bit Divider */
  926. buff[7] = pll->output_div; /* PLL Output Divider */
  927. if ((ret = mg_set_feature(mg_feature_id_transmode, mg_feature_val_trans_vcmd))
  928. != ERROR_OK)
  929. return ret;
  930. if ((ret = mg_mflash_do_write_sects(buff, 0, 1, mg_vcmd_wr_pll))
  931. != ERROR_OK)
  932. return ret;
  933. if ((ret = mg_set_feature(mg_feature_id_transmode, mg_feature_val_trans_default))
  934. != ERROR_OK)
  935. return ret;
  936. LOG_INFO("mflash: set pll ok");
  937. return ret;
  938. }
  939. static int mg_erase_nand(void)
  940. {
  941. int ret;
  942. if ((ret = mg_set_feature(mg_feature_id_transmode, mg_feature_val_trans_vcmd))
  943. != ERROR_OK)
  944. return ret;
  945. if ((ret = mg_mflash_do_write_sects(NULL, 0, 0, mg_vcmd_purge_nand))
  946. != ERROR_OK)
  947. return ret;
  948. if ((ret = mg_set_feature(mg_feature_id_transmode, mg_feature_val_trans_default))
  949. != ERROR_OK)
  950. return ret;
  951. LOG_INFO("mflash: erase nand ok");
  952. return ret;
  953. }
  954. int mg_config_cmd(struct command_context_s *cmd_ctx, char *cmd,
  955. char **args, int argc)
  956. {
  957. double fin, fout;
  958. mg_pll_t pll;
  959. int ret;
  960. if ((ret = mg_verify_interface()) != ERROR_OK)
  961. return ret;
  962. if ((ret = mg_mflash_rst()) != ERROR_OK)
  963. return ret;
  964. switch (argc) {
  965. case 2:
  966. if (!strcmp(args[1], "boot"))
  967. return mg_boot_config();
  968. else if (!strcmp(args[1], "storage"))
  969. return mg_storage_config();
  970. else
  971. return ERROR_COMMAND_NOTFOUND;
  972. break;
  973. case 3:
  974. if (!strcmp(args[1], "pll")) {
  975. fin = strtoul(args[2], NULL, 0);
  976. if (fin > MG_PLL_CLK_OUT) {
  977. LOG_ERROR("mflash: input freq. is too large");
  978. return ERROR_MG_INVALID_OSC;
  979. }
  980. fout = mg_calc_pll(fin, &pll);
  981. if (!fout) {
  982. LOG_ERROR("mflash: cannot generate valid pll");
  983. return ERROR_MG_INVALID_PLL;
  984. }
  985. LOG_INFO("mflash: Fout=%" PRIu32 " Hz, feedback=%u,"
  986. "indiv=%u, outdiv=%u, lock=%u",
  987. (uint32_t)fout, pll.feedback_div,
  988. pll.input_div, pll.output_div,
  989. pll.lock_cyc);
  990. if ((ret = mg_erase_nand()) != ERROR_OK)
  991. return ret;
  992. return mg_set_pll(&pll);
  993. } else
  994. return ERROR_COMMAND_NOTFOUND;
  995. break;
  996. default:
  997. return ERROR_COMMAND_SYNTAX_ERROR;
  998. }
  999. }
  1000. int mflash_init_drivers(struct command_context_s *cmd_ctx)
  1001. {
  1002. if (mflash_bank) {
  1003. register_command(cmd_ctx, mflash_cmd, "probe", mg_probe_cmd, COMMAND_EXEC, NULL);
  1004. register_command(cmd_ctx, mflash_cmd, "write", mg_write_cmd, COMMAND_EXEC,
  1005. "mflash write <num> <file> <address>");
  1006. register_command(cmd_ctx, mflash_cmd, "dump", mg_dump_cmd, COMMAND_EXEC,
  1007. "mflash dump <num> <file> <address> <size>");
  1008. register_command(cmd_ctx, mflash_cmd, "config", mg_config_cmd,
  1009. COMMAND_EXEC, "mflash config <num> <stage>");
  1010. }
  1011. return ERROR_OK;
  1012. }
  1013. static int mg_bank_cmd(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1014. {
  1015. target_t *target;
  1016. char *str;
  1017. int i;
  1018. if (argc < 4)
  1019. {
  1020. return ERROR_COMMAND_SYNTAX_ERROR;
  1021. }
  1022. if ((target = get_target(args[3])) == NULL)
  1023. {
  1024. LOG_ERROR("target '%s' not defined", args[3]);
  1025. return ERROR_FAIL;
  1026. }
  1027. mflash_bank = calloc(sizeof(mflash_bank_t), 1);
  1028. mflash_bank->base = strtoul(args[1], NULL, 0);
  1029. mflash_bank->rst_pin.num = strtoul(args[2], &str, 0);
  1030. if (*str)
  1031. mflash_bank->rst_pin.port[0] = (uint16_t)tolower(str[0]);
  1032. mflash_bank->target = target;
  1033. for (i = 0; mflash_gpio[i] ; i++) {
  1034. if (! strcmp(mflash_gpio[i]->name, args[0])) {
  1035. mflash_bank->gpio_drv = mflash_gpio[i];
  1036. }
  1037. }
  1038. if (! mflash_bank->gpio_drv) {
  1039. LOG_ERROR("%s is unsupported soc", args[0]);
  1040. return ERROR_MG_UNSUPPORTED_SOC;
  1041. }
  1042. return ERROR_OK;
  1043. }
  1044. int mflash_register_commands(struct command_context_s *cmd_ctx)
  1045. {
  1046. mflash_cmd = register_command(cmd_ctx, NULL, "mflash", NULL, COMMAND_ANY, NULL);
  1047. register_command(cmd_ctx, mflash_cmd, "bank", mg_bank_cmd, COMMAND_CONFIG,
  1048. "mflash bank <soc> <base> <RST pin> <target #>");
  1049. return ERROR_OK;
  1050. }