crypto_main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/cdev.h>
  5. #include <linux/fs.h>
  6. #include <linux/errno.h>
  7. #include <asm/current.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <asm/uaccess.h>
  13. //#include <stdint.h>
  14. #include "stm32f4xx.h"
  15. #include "stm32f4xx_hal_gpio.h"
  16. #include "stm32_hal_legacy.h"
  17. #include "driver.h"
  18. #include <linux/delay.h>
  19. static int major = MAJOR_CRYPTO;
  20. static int minor = 0;
  21. static dev_t devno;
  22. static struct cdev *crypto_cdev = NULL;
  23. static int count = 1;
  24. #define DEVNAME "crypto"
  25. static int crypto_open(struct inode *inode, struct file *filep);
  26. static int crypto_close(struct inode *inode, struct file *filep);
  27. static ssize_t crypto_read(struct file *filep, char __user *buf, size_t size, loff_t *offset);
  28. static ssize_t crypto_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset);
  29. static int crypto_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);
  30. static struct file_operations crypto_ops =
  31. {
  32. .owner = THIS_MODULE,
  33. .open = crypto_open,
  34. .release = crypto_close,
  35. .ioctl = crypto_ioctl,
  36. .read = crypto_read,
  37. .write = crypto_write,
  38. };
  39. #define SCL_PORT GPIOE
  40. #define SCL_PIN GPIO_PIN_6
  41. #define SDA_PORT GPIOE
  42. #define SDA_PIN GPIO_PIN_5
  43. #define SCLK_Set() HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, 1);
  44. #define SCLK_Clr() HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, 0);
  45. #define SDIN_Set() HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, 1);
  46. #define SDIN_Clr() HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, 0);
  47. void IIC_Start(void)
  48. {
  49. SCLK_Set();
  50. SDIN_Set();
  51. udelay(1);
  52. SDIN_Clr();
  53. udelay(2);
  54. SDIN_Clr();
  55. }
  56. /**********************************************
  57. //IIC Stop
  58. **********************************************/
  59. void IIC_Stop(void)
  60. {
  61. SCLK_Clr();
  62. SDIN_Clr();
  63. SCLK_Set();
  64. udelay(1);
  65. SDIN_Set();
  66. }
  67. void IIC_Ack(void)
  68. {
  69. SCLK_Clr();
  70. SDIN_Clr();
  71. udelay(1);
  72. SCLK_Set();
  73. udelay(1);
  74. SCLK_Clr();
  75. SDIN_Set(); //release SDA
  76. }
  77. void IIC_NoAck(void)
  78. {
  79. SCLK_Clr();
  80. SDIN_Set();
  81. udelay(1);
  82. SCLK_Set();
  83. udelay(1);
  84. SCLK_Clr();
  85. SDIN_Set(); //release SDA
  86. }
  87. /*
  88. 0: ACK ok
  89. 1: NACK
  90. */
  91. uint8_t IIC_Wait_Ack(void)
  92. {
  93. uint32_t IDR[10];
  94. uint32_t idr_val = 0xffffffff;
  95. int i;
  96. uint8_t ack = 0;
  97. SCLK_Clr();
  98. SDIN_Set(); //release SDA
  99. udelay(2);
  100. SCLK_Set();
  101. udelay(1);
  102. if(HAL_GPIO_ReadPin(SDA_PORT, SDA_PIN) == GPIO_PIN_SET)
  103. ack = 1;
  104. else
  105. ack = 0;
  106. udelay(1);
  107. SCLK_Clr();
  108. return ack;
  109. }
  110. void Write_IIC_Byte(unsigned char IIC_Byte)
  111. {
  112. unsigned char i;
  113. unsigned char m,da;
  114. da=IIC_Byte;
  115. SCLK_Clr();
  116. for(i=0;i<8;i++)
  117. {
  118. m=da;
  119. m=m&0x80;
  120. if(m==0x80)
  121. {
  122. SDIN_Set();
  123. }
  124. else
  125. {
  126. SDIN_Clr();
  127. }
  128. da=da<<1;
  129. udelay(1);
  130. SCLK_Set();
  131. udelay(1);
  132. SCLK_Clr();
  133. }
  134. }
  135. unsigned char Read_IIC_Byte(void)
  136. {
  137. unsigned char i;
  138. unsigned int da = 0;
  139. SCLK_Clr();
  140. SDIN_Set(); //release SDA
  141. for(i=0;i<8;i++) //8
  142. {
  143. da=da<<1 ;
  144. udelay(1);
  145. SCLK_Set();
  146. if(HAL_GPIO_ReadPin(SDA_PORT, SDA_PIN) == 1)
  147. {da++;}
  148. udelay(1);
  149. SCLK_Clr();
  150. }
  151. return da;
  152. }
  153. static int crypto_open(struct inode *inode, struct file *filep)
  154. {
  155. //__HAL_RCC_GPIOC_CLK_ENABLE();
  156. return 0;
  157. }
  158. static int crypto_close(struct inode *inode, struct file *filep)
  159. {
  160. return 0;
  161. }
  162. static int crypto_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
  163. {
  164. crypto_t crypto_arg;
  165. int i,j;
  166. if((void*)arg != NULL)
  167. {
  168. if ( copy_from_user(&crypto_arg, (void*)arg, sizeof(crypto_t)) )
  169. return -EFAULT;
  170. }
  171. switch(cmd)
  172. {
  173. case CRYPTO_WAIT_CLOCK:
  174. SDIN_Clr();
  175. for(i=0;i<crypto_arg.loop;i++)
  176. {
  177. IIC_Start();
  178. for(j=0;j<15;j++)
  179. {
  180. SCLK_Set();
  181. udelay(1);
  182. SCLK_Clr();
  183. udelay(1);
  184. }
  185. IIC_Stop();
  186. }
  187. break;
  188. case CRYPTO_SEND_COMMAND:
  189. i = 100;
  190. while(i>0)
  191. {
  192. IIC_Start();
  193. Write_IIC_Byte(crypto_arg.data[0]);
  194. if(IIC_Wait_Ack() != 0)
  195. {
  196. if(--i == 0)
  197. {
  198. printk("CRYPTO_SEND_COMMAND error!\n");
  199. return -1;
  200. }
  201. udelay(10);
  202. }
  203. else
  204. {
  205. break;
  206. }
  207. }
  208. for(i=1;i<crypto_arg.size;i++)
  209. {
  210. udelay(1);
  211. Write_IIC_Byte(crypto_arg.data[i]);
  212. if(IIC_Wait_Ack() != 0)
  213. {
  214. printk("---> CRYPTO_SEND_COMMAND ack error! i = %d\n", i);
  215. return -1;
  216. }
  217. }
  218. break;
  219. case CRYPTO_RECEIVE_DATA:
  220. if(crypto_arg.size > 0)
  221. {
  222. for(i=0;i<crypto_arg.size-1;i++)
  223. {
  224. crypto_arg.data[i] = Read_IIC_Byte();
  225. IIC_Ack();
  226. }
  227. crypto_arg.data[i] = Read_IIC_Byte();
  228. IIC_NoAck();
  229. }
  230. IIC_Stop();
  231. if ( copy_to_user((void*)arg, (void*)&crypto_arg, sizeof(crypto_t)))
  232. return -EFAULT;
  233. break;
  234. case CRYPTO_SEND_DATA:
  235. for(i=0;i<crypto_arg.size;i++)
  236. {
  237. udelay(1);
  238. Write_IIC_Byte(crypto_arg.data[i]);
  239. if(IIC_Wait_Ack() != 0)
  240. {
  241. return -1;
  242. }
  243. }
  244. IIC_Stop();
  245. break;
  246. case CRYPTO_POWER_ON:
  247. SCLK_Clr();
  248. SDIN_Set();
  249. for(i=0;i<crypto_arg.loop;i++) //CM_PWRON_CLKS
  250. {
  251. SCLK_Clr()
  252. udelay(1);
  253. SCLK_Set();
  254. udelay(1);
  255. }
  256. SDIN_Set();
  257. SCLK_Set(); //Release bus
  258. break;
  259. case CRYPTO_POWER_OFF:
  260. // udelay(1);
  261. // //SCLK_Clr();
  262. // udelay(6);
  263. IIC_Stop();
  264. SDIN_Set();
  265. SCLK_Set(); //Release bus
  266. break;
  267. case CRYPTO_SEND_CMD_BYTE:
  268. i = 100;
  269. while(i)
  270. {
  271. IIC_Start();
  272. Write_IIC_Byte(crypto_arg.data[0]);
  273. if(IIC_Wait_Ack() != 0)
  274. {
  275. if(--i == 0)
  276. {
  277. printk("CRYPTO_SEND_CMD_BYTE error!\n");
  278. return -1;
  279. }
  280. udelay(10);
  281. }
  282. else
  283. {
  284. break;
  285. }
  286. }
  287. break;
  288. default:
  289. printk("---> Invalid action\n");
  290. return -EINVAL;
  291. break;
  292. }
  293. return 0;
  294. }
  295. static ssize_t crypto_read(struct file *filep, char __user *buf, size_t size, loff_t *offset)
  296. {
  297. return 0;
  298. }
  299. static ssize_t crypto_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset)
  300. {
  301. return 0;
  302. }
  303. static void crypto_hw_init(void)
  304. {
  305. GPIO_InitTypeDef GPIO_InitStruct;
  306. //config gpio
  307. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  308. GPIO_InitStruct.Pull = GPIO_NOPULL; //GPIO_PULLDOWN; //GPIO_PULLUP;
  309. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  310. //SCL
  311. GPIO_InitStruct.Pin = SCL_PIN;
  312. HAL_GPIO_Init(SCL_PORT, &GPIO_InitStruct);
  313. //SDA
  314. GPIO_InitStruct.Pin = SDA_PIN;
  315. HAL_GPIO_Init(SDA_PORT, &GPIO_InitStruct);
  316. HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_SET);
  317. HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, GPIO_PIN_SET);
  318. }
  319. static int __init crypto_init(void)
  320. {
  321. int ret;
  322. crypto_cdev = cdev_alloc();
  323. if(crypto_cdev == NULL){
  324. return -ENOMEM;
  325. }
  326. cdev_init(crypto_cdev,&crypto_ops);
  327. devno = MKDEV(major,minor);
  328. ret = register_chrdev_region(devno, count, DEVNAME);
  329. if(ret){
  330. goto ERR_STEP;
  331. }
  332. ret = cdev_add(crypto_cdev, devno, count);
  333. if(ret){
  334. goto ERR_STEP1;
  335. }
  336. crypto_hw_init();
  337. printk("Crypto module start...\n");
  338. return 0;
  339. ERR_STEP1:
  340. unregister_chrdev_region(devno,count);
  341. ERR_STEP:
  342. cdev_del(crypto_cdev);
  343. return ret;
  344. }
  345. static void __exit crypto_exit(void)
  346. {
  347. unregister_chrdev_region(MKDEV(major,minor),count);
  348. cdev_del(crypto_cdev);
  349. }
  350. module_init(crypto_init);
  351. module_exit(crypto_exit);
  352. MODULE_LICENSE("GPL");
  353. MODULE_AUTHOR("Jimbo");
  354. MODULE_DESCRIPTION("this is crypto AT88SC0104C module");