emmc_otp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. #include <generated/autoconf.h>
  2. #include <linux/module.h>
  3. #include <linux/moduleparam.h>
  4. #include <linux/init.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/timer.h>
  7. #include <linux/ioport.h>
  8. #include <linux/device.h>
  9. #include <linux/miscdevice.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/delay.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/core.h>
  17. #include <linux/mmc/mmc.h>
  18. #include <linux/mmc/sd.h>
  19. #include <linux/mmc/sdio.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/fs.h>
  23. #include <asm/uaccess.h>
  24. #include "mt_sd.h"
  25. #include "emmc_otp.h"
  26. #include <mt-plat/partition.h>
  27. #define DRV_NAME_MISC "otp"
  28. #define PROCNAME "driver/otp"
  29. #ifndef CONFIG_MTK_EMMC_SUPPORT_OTP
  30. #define CONFIG_MTK_EMMC_SUPPORT_OTP
  31. #endif
  32. #define EMMC_OTP_DEBUG 1
  33. /* static spinlock_t g_emmc_otp_lock; */
  34. static struct emmc_otp_config g_emmc_otp_func;
  35. static unsigned int sg_wp_size; /* byte unit */
  36. static unsigned int g_otp_user_ccci = 2 * 1024 * 1024;
  37. struct msdc_host *emmc_otp_get_host(void)
  38. {
  39. return mtk_msdc_host[EMMC_HOST_NUM];
  40. }
  41. /******************************************************************************
  42. * EMMC OTP operations
  43. * ***************************************************************************/
  44. unsigned int emmc_get_wp_size(void)
  45. {
  46. unsigned char l_ext_csd[512];
  47. struct msdc_host *host_ctl;
  48. u32 *resp = NULL;
  49. unsigned int write_prot_grpsz = 0;
  50. /* not to change ERASE_GRP_DEF after card initialized */
  51. host_ctl = emmc_otp_get_host();
  52. BUG_ON(!host_ctl);
  53. BUG_ON(!host_ctl->mmc);
  54. BUG_ON(!host_ctl->mmc->card);
  55. if (0 != sg_wp_size) {
  56. pr_debug("msdc[%s:%d]mmc_host = 0x%p,sg_wp_size=%d\n", __func__,
  57. __LINE__, host_ctl->mmc, sg_wp_size);
  58. return sg_wp_size;
  59. }
  60. pr_debug("[%s:%d]mmc_host = 0x%p\n", __func__, __LINE__, host_ctl->mmc);
  61. mmc_claim_host(host_ctl->mmc);
  62. /* pr_debug("[%s:%d]claim host done!\n", __func__, __LINE__);
  63. * pr_debug("claim_status=%d, claim_cnt=%d, claimer=0x%x, current=0x%x\n",
  64. * host_ctl->mmc->claimed, host_ctl->mmc->claim_cnt, host_ctl->mmc->claimer
  65. * , current);
  66. */
  67. /*
  68. * As the ext_csd is so large and mostly unused, we don't store the
  69. * raw block in mmc_card, so re-get it here.
  70. */
  71. mmc_send_ext_csd(host_ctl->mmc->card, l_ext_csd);
  72. #if EMMC_OTP_DEBUG
  73. {
  74. int i;
  75. for (i = 0; i < 512; i++) {
  76. pr_debug("%x", l_ext_csd[i]);
  77. if (0 == ((i + 1) % 16))
  78. pr_debug("\n");
  79. }
  80. }
  81. #endif
  82. mmc_release_host(host_ctl->mmc);
  83. /* otp length equal to one write protect group size */
  84. if (l_ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x1) {
  85. /* use high-capacity erase uint size, hc erase timeout, hc wp size,
  86. * store in EXT_CSD */
  87. sg_wp_size =
  88. (512 * 1024 * l_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
  89. l_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]);
  90. } else {
  91. resp = host_ctl->mmc->card->raw_csd;
  92. write_prot_grpsz = UNSTUFF_BITS(resp, 32, 5);
  93. /* use old erase group size,write protect group size, store in CSD*/
  94. sg_wp_size = (512 * host_ctl->mmc->card->erase_size)*
  95. (write_prot_grpsz + 1);
  96. }
  97. pr_debug("write_protect_group_size = %d MByte %s:%d\n", sg_wp_size / 1048576);
  98. return sg_wp_size;
  99. }
  100. /* need subtract emmc reserved region for combo feature */
  101. unsigned int emmc_otp_start(void)
  102. {
  103. unsigned int l_addr;
  104. /* unsigned int l_offset;*/ /* for emmc combo reserved */
  105. /* unsigned int l_reserve;*/
  106. struct msdc_host *host_ctl;
  107. struct hd_struct *lp_hd_struct = NULL;
  108. if (0 == sg_wp_size)
  109. emmc_get_wp_size();
  110. host_ctl = emmc_otp_get_host();
  111. BUG_ON(!host_ctl);
  112. BUG_ON(!host_ctl->mmc);
  113. BUG_ON(!host_ctl->mmc->card);
  114. lp_hd_struct = get_part("otp");
  115. if (NULL == lp_hd_struct) {
  116. pr_debug("not find otp in scatter file error!\n");
  117. put_part(lp_hd_struct);
  118. return -EFAULT;
  119. }
  120. l_addr = lp_hd_struct->start_sect; /* block unit */
  121. /* find wp group start address in 43MB reserved region */
  122. if (l_addr % (sg_wp_size >> 9))
  123. l_addr += ((sg_wp_size >> 9) - l_addr % (sg_wp_size >> 9));
  124. if (NULL != lp_hd_struct)
  125. put_part(lp_hd_struct);
  126. pr_debug("find OTP start address is 0x%x\n", l_addr);
  127. return l_addr;
  128. }
  129. unsigned int emmc_otp_query_length(unsigned int *QLength)
  130. {
  131. /* otp length equal to one write protect group size */
  132. *QLength = emmc_get_wp_size() - g_otp_user_ccci;
  133. return 0;
  134. }
  135. unsigned int emmc_otp_read(unsigned int blk_offset, void *BufferPtr)
  136. {
  137. char l_buf[512];
  138. unsigned char *l_rcv_buf = (unsigned char *)BufferPtr;
  139. unsigned int l_addr;
  140. unsigned int l_otp_size;
  141. unsigned int l_ret;
  142. struct scatterlist msdc_sg;
  143. struct mmc_data msdc_data;
  144. struct mmc_command msdc_cmd;
  145. struct mmc_request msdc_mrq;
  146. struct msdc_host *host_ctl;
  147. /* check parameter */
  148. l_addr = emmc_otp_start();
  149. l_otp_size = emmc_get_wp_size();
  150. if (blk_offset > (l_otp_size >> 9))
  151. return OTP_ERROR_OVERSCOPE;
  152. l_addr += blk_offset;
  153. host_ctl = emmc_otp_get_host();
  154. BUG_ON(!host_ctl);
  155. BUG_ON(!host_ctl->mmc);
  156. pr_debug("[%s:%d]mmc_host is : 0x%p\n", __func__, __LINE__, host_ctl->mmc);
  157. mmc_claim_host(host_ctl->mmc);
  158. /* make sure access user data area */
  159. mmc_send_ext_csd(host_ctl->mmc->card, l_buf);
  160. if (l_buf[179] & 0x7) {
  161. l_buf[179] &= ~0x7;
  162. l_buf[179] |= 0x0;
  163. mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
  164. }
  165. #if EMMC_OTP_DEBUG
  166. pr_debug("EMMC_OTP: start MSDC_SINGLE_READ_WRITE!\n");
  167. #endif
  168. memset(&msdc_data, 0, sizeof(struct mmc_data));
  169. memset(&msdc_mrq, 0, sizeof(struct mmc_request));
  170. memset(&msdc_cmd, 0, sizeof(struct mmc_command));
  171. msdc_mrq.cmd = &msdc_cmd;
  172. msdc_mrq.data = &msdc_data;
  173. msdc_data.flags = MMC_DATA_READ;
  174. msdc_cmd.opcode = MMC_READ_SINGLE_BLOCK;
  175. msdc_data.blocks = 1; /* one block Request */
  176. memset(l_rcv_buf, 0, 512);
  177. msdc_cmd.arg = l_addr;
  178. BUG_ON(!host_ctl->mmc->card);
  179. if (!mmc_card_blockaddr(host_ctl->mmc->card)) {
  180. pr_debug("the device is used byte address!\n");
  181. msdc_cmd.arg <<= 9;
  182. }
  183. msdc_cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  184. msdc_data.stop = NULL;
  185. msdc_data.blksz = 512;
  186. msdc_data.sg = &msdc_sg;
  187. msdc_data.sg_len = 1;
  188. sg_init_one(&msdc_sg, l_rcv_buf, 512);
  189. mmc_set_data_timeout(&msdc_data, host_ctl->mmc->card);
  190. mmc_wait_for_req(host_ctl->mmc, &msdc_mrq);
  191. mmc_release_host(host_ctl->mmc);
  192. if (msdc_cmd.error)
  193. l_ret = msdc_cmd.error;
  194. if (msdc_data.error)
  195. l_ret = msdc_data.error;
  196. else
  197. l_ret = OTP_SUCCESS;
  198. return l_ret;
  199. }
  200. unsigned int emmc_otp_write(unsigned int blk_offset, void *BufferPtr)
  201. {
  202. char l_buf[512];
  203. unsigned char *l_send_buf = (unsigned char *)BufferPtr;
  204. unsigned int l_ret;
  205. unsigned int l_addr;
  206. unsigned int l_otp_size;
  207. struct scatterlist msdc_sg;
  208. struct mmc_data msdc_data;
  209. struct mmc_command msdc_cmd;
  210. struct mmc_request msdc_mrq;
  211. struct msdc_host *host_ctl;
  212. #ifdef MTK_MSDC_USE_CACHE
  213. struct mmc_command msdc_sbc;
  214. #endif
  215. /* check parameter */
  216. l_addr = emmc_otp_start();
  217. l_otp_size = emmc_get_wp_size();
  218. if (blk_offset > (l_otp_size >> 9))
  219. return OTP_ERROR_OVERSCOPE;
  220. l_addr += blk_offset;
  221. host_ctl = emmc_otp_get_host();
  222. BUG_ON(!host_ctl);
  223. BUG_ON(!host_ctl->mmc);
  224. mmc_claim_host(host_ctl->mmc);
  225. /* make sure access user data area */
  226. mmc_send_ext_csd(host_ctl->mmc->card, l_buf);
  227. if (l_buf[179] & 0x7) {
  228. l_buf[179] &= ~0x7;
  229. l_buf[179] |= 0x0;
  230. mmc_switch(host_ctl->mmc->card, 0, 179, l_buf[179], 1000);
  231. }
  232. #if EMMC_OTP_DEBUG
  233. pr_debug("EMMC_OTP: start MSDC_SINGLE_READ_WRITE!\n");
  234. #endif
  235. memset(&msdc_data, 0, sizeof(struct mmc_data));
  236. memset(&msdc_mrq, 0, sizeof(struct mmc_request));
  237. memset(&msdc_cmd, 0, sizeof(struct mmc_command));
  238. #ifdef MTK_MSDC_USE_CACHE
  239. memset(&msdc_sbc, 0, sizeof(struct mmc_command));
  240. #endif
  241. msdc_mrq.cmd = &msdc_cmd;
  242. msdc_mrq.data = &msdc_data;
  243. msdc_data.flags = MMC_DATA_WRITE;
  244. #ifdef MTK_MSDC_USE_CACHE
  245. if (mmc_card_mmc(host_ctl->mmc->card) && (host_ctl->mmc->card->ext_csd.cache_ctrl & 0x1)) {
  246. msdc_cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
  247. msdc_mrq.sbc = &msdc_sbc;
  248. msdc_mrq.sbc->opcode = MMC_SET_BLOCK_COUNT;
  249. msdc_mrq.sbc->arg = msdc_data.blocks | (1 << 31);
  250. msdc_mrq.sbc->flags = MMC_RSP_R1 | MMC_CMD_AC;
  251. } else {
  252. msdc_cmd.opcode = MMC_WRITE_BLOCK;
  253. }
  254. #else
  255. msdc_cmd.opcode = MMC_WRITE_BLOCK;
  256. #endif
  257. msdc_data.blocks = 1;
  258. msdc_cmd.arg = l_addr;
  259. BUG_ON(!host_ctl->mmc->card);
  260. if (!mmc_card_blockaddr(host_ctl->mmc->card)) {
  261. pr_debug("the device is used byte address!\n");
  262. msdc_cmd.arg <<= 9;
  263. }
  264. msdc_cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  265. msdc_data.stop = NULL;
  266. msdc_data.blksz = 512;
  267. msdc_data.sg = &msdc_sg;
  268. msdc_data.sg_len = 1;
  269. sg_init_one(&msdc_sg, l_send_buf, 512);
  270. mmc_set_data_timeout(&msdc_data, host_ctl->mmc->card);
  271. mmc_wait_for_req(host_ctl->mmc, &msdc_mrq);
  272. mmc_release_host(host_ctl->mmc);
  273. if (msdc_cmd.error)
  274. l_ret = msdc_cmd.error;
  275. if (msdc_data.error)
  276. l_ret = msdc_data.error;
  277. else
  278. l_ret = OTP_SUCCESS;
  279. return l_ret;
  280. }
  281. /*
  282. static int emmc_otp_proc_read(char *page, char **start, off_t off,
  283. int count, int *eof, void *data)
  284. {
  285. int len;
  286. if (off > 0)
  287. {
  288. return 0;
  289. }
  290. return len;
  291. }
  292. static int emmc_otp_proc_write(struct file* file, const char* buffer,
  293. unsigned long count, void *data)
  294. {
  295. char buf[16];
  296. int len = count;
  297. if (len >= sizeof(buf))
  298. {
  299. len = sizeof(buf) - 1;
  300. }
  301. if (copy_from_user(buf, buffer, len))
  302. {
  303. return -EFAULT;
  304. }
  305. return len;
  306. }
  307. */
  308. static int mt_otp_open(struct inode *inode, struct file *filp)
  309. {
  310. pr_debug("[%s]:(MAJOR)%d:(MINOR)%d\n",
  311. __func__, MAJOR(inode->i_rdev), MINOR(inode->i_rdev));
  312. filp->private_data = (int *)OTP_MAGIC_NUM;
  313. return 0;
  314. }
  315. static int mt_otp_release(struct inode *inode, struct file *filp)
  316. {
  317. pr_debug("[%s]:(MAJOR)%d:(MINOR)%d\n",
  318. __func__, MAJOR(inode->i_rdev), MINOR(inode->i_rdev));
  319. return 0;
  320. }
  321. static int mt_otp_access(unsigned int access_type, unsigned int offset, void *buff_ptr,
  322. unsigned int length, unsigned int *status)
  323. {
  324. unsigned int ret = 0;
  325. char *BufAddr = (char *)buff_ptr;
  326. unsigned int blkno, AccessLength = 0;
  327. unsigned int l_block_size = 512;
  328. int Status = 0;
  329. char *p_D_Buff;
  330. /* char S_Buff[64]; */
  331. p_D_Buff = kmalloc(l_block_size, GFP_KERNEL);
  332. if (!p_D_Buff) {
  333. ret = -ENOMEM;
  334. *status = OTP_ERROR_NOMEM;
  335. goto exit;
  336. }
  337. pr_debug("[%s]: %s (0x%x) length:(%d bytes) !\n",
  338. __func__, access_type ? "WRITE" : "READ", offset, length);
  339. do {
  340. blkno = offset / l_block_size;
  341. if (FS_EMMC_OTP_READ == access_type) {
  342. memset(p_D_Buff, 0xff, l_block_size);
  343. pr_debug("[%s]: Read Access of page (%d)\n", __func__, blkno);
  344. Status = g_emmc_otp_func.read(blkno, p_D_Buff);
  345. *status = Status;
  346. if (OTP_SUCCESS != Status) {
  347. pr_debug("[%s]: Read status (%d)\n", __func__, Status);
  348. break;
  349. }
  350. AccessLength = l_block_size - (offset % l_block_size);
  351. if (length >= AccessLength) {
  352. memcpy(BufAddr, (p_D_Buff + (offset % l_block_size)), AccessLength);
  353. } else {
  354. /* last time */
  355. memcpy(BufAddr, (p_D_Buff + (offset % l_block_size)), length);
  356. }
  357. } else if (FS_EMMC_OTP_WRITE == access_type) {
  358. AccessLength = l_block_size - (offset % l_block_size);
  359. memset(p_D_Buff, 0x0, l_block_size);
  360. Status = g_emmc_otp_func.read(blkno, p_D_Buff);
  361. *status = Status;
  362. if (OTP_SUCCESS != Status) {
  363. pr_debug("[%s]: read before write, Read status (%d) blkno (0x%x)\n",
  364. __func__, Status, blkno);
  365. break;
  366. }
  367. if (length >= AccessLength) {
  368. memcpy((p_D_Buff + (offset % l_block_size)), BufAddr, AccessLength);
  369. } else {
  370. /* last time */
  371. memcpy((p_D_Buff + (offset % l_block_size)), BufAddr, length);
  372. }
  373. Status = g_emmc_otp_func.write(blkno, p_D_Buff);
  374. *status = Status;
  375. if (OTP_SUCCESS != Status) {
  376. pr_debug("[%s]: Write status (%d)\n", __func__, Status);
  377. break;
  378. }
  379. } else {
  380. pr_err("[%s]: Error, not either read nor write operations !\n", __func__);
  381. break;
  382. }
  383. offset += AccessLength;
  384. BufAddr += AccessLength;
  385. if (length <= AccessLength) {
  386. length = 0;
  387. break;
  388. }
  389. length -= AccessLength;
  390. pr_debug("[%s]: Remaining %s (%d) !\n", __func__,
  391. access_type ? "WRITE" : "READ", length);
  392. } while (1);
  393. kfree(p_D_Buff);
  394. exit:
  395. return ret;
  396. }
  397. static long mt_otp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  398. {
  399. int ret = 0;
  400. char *pbuf;
  401. void __user *uarg = (void __user *)arg;
  402. struct otp_ctl otpctl;
  403. /* Lock */
  404. /* spin_lock(&g_emmc_otp_lock); */
  405. if (copy_from_user(&otpctl, uarg, sizeof(struct otp_ctl))) {
  406. ret = -EFAULT;
  407. goto exit;
  408. }
  409. /*
  410. if(false == g_bInitDone)
  411. {
  412. pr_err("ERROR: EMMC Flash Not initialized !!\n");
  413. ret = -EFAULT;
  414. goto exit;
  415. } */
  416. pbuf = kmalloc_array(otpctl.Length, sizeof(char), GFP_KERNEL);
  417. if (!pbuf) {
  418. ret = -ENOMEM;
  419. goto exit;
  420. }
  421. switch (cmd) {
  422. case EMMC_OTP_GET_LENGTH:
  423. pr_debug("OTP IOCTL: EMMC_OTP_GET_LENGTH\n");
  424. if (('c' == (otpctl.status & 0xff)) && ('c' == ((otpctl.status >> 8) & 0xff))
  425. && ('c' == ((otpctl.status >> 16) & 0xff))
  426. && ('i' == ((otpctl.status >> 24) & 0xff))) {
  427. otpctl.QLength = g_otp_user_ccci;
  428. } else {
  429. g_emmc_otp_func.query_length(&otpctl.QLength);
  430. }
  431. otpctl.status = OTP_SUCCESS;
  432. pr_debug("OTP IOCTL: The Length is %d\n", otpctl.QLength);
  433. break;
  434. case EMMC_OTP_READ:
  435. pr_debug("OTP IOCTL: EMMC_OTP_READ Offset(0x%x), Length(0x%x)\n", otpctl.Offset,
  436. otpctl.Length);
  437. memset(pbuf, 0xff, sizeof(char) * otpctl.Length);
  438. mt_otp_access(FS_EMMC_OTP_READ, otpctl.Offset, pbuf, otpctl.Length, &otpctl.status);
  439. if (copy_to_user(otpctl.BufferPtr, pbuf, (sizeof(char) * otpctl.Length))) {
  440. pr_err("EMMC_OTP IOCTL: Copy to user buffer Error !\n");
  441. goto error;
  442. }
  443. break;
  444. case EMMC_OTP_WRITE:
  445. pr_debug("OTP IOCTL: EMMC_OTP_WRITE Offset(0x%x), Length(0x%x)\n", otpctl.Offset,
  446. otpctl.Length);
  447. if (copy_from_user(pbuf, otpctl.BufferPtr, (sizeof(char) * otpctl.Length))) {
  448. pr_err("EMMC_OTP IOCTL: Copy from user buffer Error !\n");
  449. goto error;
  450. }
  451. mt_otp_access(FS_EMMC_OTP_WRITE, otpctl.Offset, pbuf, otpctl.Length,
  452. &otpctl.status);
  453. break;
  454. default:
  455. ret = -EINVAL;
  456. }
  457. ret = copy_to_user(uarg, &otpctl, sizeof(struct otp_ctl));
  458. error:
  459. kfree(pbuf);
  460. exit:
  461. /* spin_unlock(&g_emmc_otp_lock); */
  462. return ret;
  463. }
  464. #ifdef CONFIG_COMPAT
  465. static int compat_get_otp_ctl_allocation(struct compat_otp_ctl __user *arg32,
  466. struct otp_ctl __user *arg64)
  467. {
  468. compat_uint_t u;
  469. compat_uptr_t p;
  470. int err;
  471. err = get_user(u, &(arg32->QLength));
  472. err |= put_user(u, &(arg64->QLength));
  473. err |= get_user(u, &(arg32->Offset));
  474. err |= put_user(u, &(arg64->Offset));
  475. err |= get_user(u, &(arg32->Length));
  476. err |= put_user(u, &(arg64->Length));
  477. err |= get_user(p, &(arg32->BufferPtr));
  478. err |= put_user(compat_ptr(p), &(arg64->BufferPtr));
  479. err |= get_user(u, &(arg32->status));
  480. err |= put_user(u, &(arg64->status));
  481. return err;
  482. }
  483. static int compat_put_otp_ctl_allocation(struct compat_otp_ctl __user *arg32,
  484. struct otp_ctl __user *arg64)
  485. {
  486. compat_uint_t u;
  487. int err;
  488. err = get_user(u, &(arg64->QLength));
  489. err |= put_user(u, &(arg32->QLength));
  490. err |= get_user(u, &(arg64->Offset));
  491. err |= put_user(u, &(arg32->Offset));
  492. err |= get_user(u, &(arg64->Length));
  493. err |= put_user(u, &(arg32->Length));
  494. err |= get_user(u, &(arg64->status));
  495. err |= put_user(u, &(arg32->status));
  496. return err;
  497. }
  498. static long mt_otp_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  499. {
  500. int ret = 0;
  501. int err = 0;
  502. struct compat_otp_ctl *arg32;
  503. struct otp_ctl *arg64;
  504. /* void __user *uarg = compat_ptr(arg); */
  505. if (!file->f_op || !file->f_op->unlocked_ioctl)
  506. return -ENOTTY;
  507. arg32 = compat_ptr(arg);
  508. arg64 = compat_alloc_user_space(sizeof(*arg64));
  509. if (arg64 == NULL)
  510. return -EFAULT;
  511. ret = compat_get_otp_ctl_allocation(arg32, arg64);
  512. if (ret)
  513. return ret;
  514. /* Lock */
  515. /* spin_lock(&g_emmc_otp_lock); */
  516. switch (cmd) {
  517. case COMPAT_EMMC_OTP_GET_LENGTH:
  518. ret = mt_otp_ioctl(file, EMMC_OTP_GET_LENGTH, (unsigned long)arg64);
  519. /* pr_err("OTP IOCTL: COMPAT_EMMC_OTP_GET_LENGTH\n"); */
  520. if (ret)
  521. pr_err("EMMC_OTP_GET_LENGTH unlocked_ioctl failed.");
  522. break;
  523. case COMPAT_EMMC_OTP_READ:
  524. /* pr_err("OTP IOCTL: COMPAT_EMMC_OTP_READ\n"); */
  525. ret = mt_otp_ioctl(file, EMMC_OTP_READ, (unsigned long)arg64);
  526. if (ret)
  527. pr_err("EMMC_OTP_READ unlocked_ioctl failed.");
  528. break;
  529. case COMPAT_EMMC_OTP_WRITE:
  530. /* pr_err("OTP IOCTL: COMPAT_EMMC_OTP_WRITE\n"); */
  531. ret = mt_otp_ioctl(file, EMMC_OTP_WRITE, (unsigned long)arg64);
  532. if (ret)
  533. pr_err("EMMC_OTP_WRITE unlocked_ioctl failed.");
  534. break;
  535. default:
  536. ret = -EINVAL;
  537. }
  538. err = compat_put_otp_ctl_allocation(arg32, arg64);
  539. return ret ? ret : err;
  540. }
  541. #endif
  542. static const struct file_operations emmc_otp_fops = {
  543. .owner = THIS_MODULE,
  544. .unlocked_ioctl = mt_otp_ioctl,
  545. .open = mt_otp_open,
  546. .release = mt_otp_release,
  547. #ifdef CONFIG_COMPAT
  548. .compat_ioctl = mt_otp_ioctl_compat,
  549. #endif
  550. };
  551. static struct miscdevice emmc_otp_dev = {
  552. .minor = MISC_DYNAMIC_MINOR,
  553. .name = "otp",
  554. .fops = &emmc_otp_fops,
  555. };
  556. static int emmc_otp_probe(struct platform_device *pdev)
  557. {
  558. int ret = 0;
  559. pr_debug("in emmc otp probe function\n");
  560. return ret;
  561. }
  562. static int emmc_otp_remove(struct platform_device *pdev)
  563. {
  564. return 0;
  565. }
  566. static struct platform_driver emmc_otp_driver = {
  567. .probe = emmc_otp_probe,
  568. .remove = emmc_otp_remove,
  569. .driver = {
  570. .name = DRV_NAME_MISC,
  571. .owner = THIS_MODULE,
  572. },
  573. };
  574. static int __init emmc_otp_init(void)
  575. {
  576. int err = 0;
  577. pr_debug("MediaTek EMMC OTP misc driver init\n");
  578. /* spin_lock_init(&g_emmc_otp_lock); */
  579. g_emmc_otp_func.query_length = emmc_otp_query_length;
  580. g_emmc_otp_func.read = emmc_otp_read;
  581. g_emmc_otp_func.write = emmc_otp_write;
  582. #if 0
  583. #ifndef USER_BUILD_KERNEL
  584. entry = create_proc_entry(PROCNAME, 0660, NULL);
  585. #else
  586. entry = create_proc_entry(PROCNAME, 0440, NULL);
  587. #endif
  588. if (entry == NULL) {
  589. pr_err("emmc OTP: unable to create /proc entry\n");
  590. return -ENOMEM;
  591. }
  592. entry->read_proc = emmc_otp_proc_read;
  593. entry->write_proc = emmc_otp_proc_write;
  594. /* entry->owner = THIS_MODULE; */
  595. #endif
  596. platform_driver_register(&emmc_otp_driver);
  597. pr_debug("OTP: register EMMC OTP device ...\n");
  598. err = misc_register(&emmc_otp_dev);
  599. if (unlikely(err)) {
  600. pr_err("OTP: failed to register EMMC OTP device!\n");
  601. return err;
  602. }
  603. return 0;
  604. }
  605. static void __exit emmc_otp_exit(void)
  606. {
  607. pr_debug("MediaTek EMMC OTP misc driver exit\n");
  608. misc_deregister(&emmc_otp_dev);
  609. g_emmc_otp_func.query_length = NULL;
  610. g_emmc_otp_func.read = NULL;
  611. g_emmc_otp_func.write = NULL;
  612. platform_driver_unregister(&emmc_otp_driver);
  613. remove_proc_entry(PROCNAME, NULL);
  614. }
  615. module_init(emmc_otp_init);
  616. module_exit(emmc_otp_exit);