uartb.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/device.h>
  4. #include <linux/kernel.h>
  5. #include <linux/slab.h>
  6. #include <linux/fs.h>
  7. #include <linux/cdev.h>
  8. #include <asm/irq.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/io.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/kfifo.h>
  14. #include <linux/version.h>
  15. #include <linux/spinlock.h>
  16. #include "drv_devices.h"
  17. #include "drv_uartb.h"
  18. #define IRQ_UARTB 54
  19. struct kfifo uartb_fifo;
  20. DEFINE_SPINLOCK(uart_write_lock);
  21. static void __set_baudrate_low(unsigned int baudrate)
  22. {
  23. unsigned int dl;
  24. dl = baudrate * 64 / 375;
  25. *(volatile unsigned int *) 0xbe0d0114 = dl;
  26. clear_bit(15,(volatile void __iomem *)0xbe00002c);
  27. }
  28. static void __set_baudrate_high(unsigned int baudrate)
  29. {
  30. #if defined(CONFIG_CHIP_8503)
  31. if (baudrate == 230400)
  32. *(unsigned char *)0xbe00018e = 0xff;
  33. else if (baudrate == 460800)
  34. *(unsigned char *)0xbe00018e = 0xbf;
  35. else if (baudrate == 921600)
  36. *(unsigned char *)0xbe00018e = 0x9f;
  37. #elif defined(CONFIG_CHIP_8506)
  38. if (baudrate == 230400)
  39. return;
  40. else if (baudrate == 460800)
  41. *(unsigned char *)0xbe00018e = 0xfa;
  42. else if (baudrate == 921600)
  43. *(unsigned char *)0xbe00018e = 0xbc;
  44. #elif defined(CONFIG_CHIP_6710)
  45. if (baudrate == 230400)
  46. return;
  47. else if (baudrate == 460800)
  48. *(unsigned char *)0xbe00018e = 0xe1;
  49. else if (baudrate == 921600)
  50. *(unsigned char *)0xbe00018e = 0xb0;
  51. #elif defined(CONFIG_CHIP_8501)
  52. if (baudrate == 230400)
  53. return;
  54. else if (baudrate == 460800)
  55. *(unsigned char *)0xbe00018e = 0xd5;
  56. else if (baudrate == 921600)
  57. *(unsigned char *)0xbe00018e = 0xb5;
  58. #elif defined(CONFIG_CHIP_533) || defined(CONFIG_CHIP_512L)
  59. if (baudrate == 230400)
  60. return;
  61. else if (baudrate == 460800)
  62. *(unsigned char *)0xbe00018e = 0xe2;
  63. else if (baudrate == 921600)
  64. *(unsigned char *)0xbe00018e = 0xb1;
  65. #endif
  66. set_bit(15,(volatile void __iomem *)0xbe00002c);
  67. }
  68. static void uartb_set_baudrate(unsigned int baudrate)
  69. {
  70. unsigned long flags;
  71. spin_lock_irqsave(&uart_write_lock, flags);
  72. switch (baudrate) {
  73. case 9600:
  74. case 14400:
  75. case 19200:
  76. case 38400:
  77. case 57600:
  78. case 115200:
  79. __set_baudrate_low(baudrate);
  80. break;
  81. case 230400:
  82. case 460800:
  83. case 921600:
  84. __set_baudrate_high(baudrate);
  85. break;
  86. }
  87. spin_unlock_irqrestore(&uart_write_lock, flags);
  88. }
  89. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,8)
  90. long uartb_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  91. #else
  92. static ssize_t uartb_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  93. #endif
  94. {
  95. switch (cmd) {
  96. case UARTB_SET_BAUDRATE:
  97. uartb_set_baudrate(arg);
  98. break;
  99. }
  100. return 0;
  101. }
  102. static ssize_t uartb_write(struct file *filp, const char __user *uBuff, size_t count, loff_t *f_pos)
  103. {
  104. unsigned char *kBuff;
  105. unsigned int off_irq_flag=0;
  106. off_irq_flag = (unsigned int)count & 0x8000;
  107. count &= 0x7fff;
  108. if((kBuff=(unsigned char *)kmalloc(count, GFP_KERNEL))==NULL)
  109. {
  110. printk(KERN_EMERG"audiorx_write: no enough memory to do !!!\n");
  111. return count;
  112. }
  113. if(copy_from_user(kBuff, uBuff, count))
  114. {
  115. printk(KERN_EMERG"audiorx_write: copy_from_user error !!!\n");
  116. }
  117. else
  118. {
  119. unsigned int i;
  120. unsigned long flags;
  121. if (off_irq_flag)
  122. spin_lock_irqsave(&uart_write_lock, flags);
  123. for (i = 0; i < count; i++) {
  124. while(! ((*(volatile unsigned int *)0xbe0d0110)&0x00006000));
  125. writeb(kBuff[i], (volatile unsigned char *)0xbe0d0104);
  126. }
  127. if (off_irq_flag)
  128. spin_unlock_irqrestore(&uart_write_lock, flags);
  129. //AI_write(kBuff, count);
  130. }
  131. kfree(kBuff);
  132. return count;
  133. }
  134. static ssize_t uartb_read(struct file *filp, char __user *uBuff, size_t count, loff_t *f_pos)
  135. {
  136. size_t i,ret;
  137. size_t max_count;
  138. unsigned char tmp_buf[512];
  139. memset(tmp_buf,0x0,sizeof(tmp_buf));
  140. max_count = (count < 512)? count: 512;
  141. //printk("<0> max_count = %d\n",max_count);
  142. for (i = 0; i < max_count; i++) {
  143. if (kfifo_is_empty(&uartb_fifo) != 0) {
  144. //printk("<0>kfifo_is_empty\n");
  145. break;
  146. }
  147. ret = kfifo_out(&uartb_fifo, &tmp_buf[i], sizeof(unsigned char));
  148. //printk("<0>c=%c\n",tmp_buf[i]);
  149. if (ret != sizeof(unsigned char))
  150. return -EINVAL;
  151. }
  152. if (copy_to_user(uBuff, tmp_buf, count)) {
  153. printk("<0> copy_to_user error!\n");
  154. i = -EFAULT;
  155. }
  156. return i;
  157. }
  158. static int uartb_open(struct inode *i, struct file *f)
  159. {
  160. printk("<0>file open\n");
  161. kfifo_reset(&uartb_fifo);
  162. #if defined(CONFIG_XHDMICPIN_TO_UARTB)
  163. //UARTB output from XHDMIC_SDA & XHDMIC_SCL
  164. set_bit(2, (volatile void __iomem *)0xbe000010);
  165. #elif defined(CONFIG_KEY1_I2S_SCK_TO_UARTB)
  166. //KEY1 as UARTB_RX
  167. clear_bit(20, (volatile void __iomem *) 0xbe0f0600);
  168. set_bit(21, (volatile void __iomem *)0xbe0f0600);
  169. //I2C_SCK as UARTB_TX
  170. clear_bit(2, (volatile void __iomem *) 0xbe0f0600);
  171. clear_bit(3, (volatile void __iomem *) 0xbe0f0600);
  172. set_bit(15, (volatile void __iomem *) 0xbe00012c);
  173. clear_bit(8, (volatile void __iomem *) 0xbe00013c);//R_I2S_OEJ
  174. clear_bit(26, (volatile void __iomem *) 0xbe00013c);
  175. #else
  176. //LVB_TXN4 as UARTB_RX
  177. set_bit(14, (volatile void __iomem *) 0xbe00012c);
  178. #if (CONFIG_CHIPID == 0x533)
  179. set_bit(9, (volatile void __iomem *) 0xbe0000ec);
  180. #else
  181. clear_bit(9, (volatile void __iomem *) 0xbe00025c);
  182. clear_bit(17, (volatile void __iomem *) 0xbe00025c);
  183. #endif
  184. //LVA_TXN4 as UARTB_TX
  185. set_bit(14, (volatile void __iomem *) 0xbe00012c);
  186. #if (CONFIG_CHIPID == 0x533)
  187. clear_bit(4, (volatile void __iomem *) 0xbe0000ec);
  188. #else
  189. clear_bit(4, (volatile void __iomem *) 0xbe00025c);
  190. clear_bit(17, (volatile void __iomem *) 0xbe00025c);
  191. #endif
  192. #endif
  193. return 0;
  194. }
  195. static int uartb_close(struct inode *i, struct file *f)
  196. {
  197. printk("<0>uartb_close\n");
  198. #ifdef CONFIG_XHDMICPIN_TO_UARTB
  199. clear_bit(2, (volatile void __iomem *) 0xbe000010);
  200. #elif defined(CONFIG_KEY1_I2S_SCK_TO_UARTB)
  201. clear_bit(21, (volatile void __iomem *)0xbe0f0600);
  202. set_bit(8, (volatile void __iomem *) 0xbe00013c);
  203. clear_bit(15, (volatile void __iomem *) 0xbe00012c);
  204. #else
  205. clear_bit(14, (volatile void __iomem *) 0xbe00012c);
  206. #if (CONFIG_CHIPID == 0x533)
  207. clear_bit(9, (volatile void __iomem *) 0xbe0000ec);
  208. #else
  209. set_bit(9, (volatile void __iomem *) 0xbe00025c);
  210. set_bit(4, (volatile void __iomem *) 0xbe00025c);
  211. #endif
  212. #endif
  213. return 0;
  214. }
  215. static irqreturn_t uartb_ISR( int irq, void* dev_id )
  216. {
  217. int i;
  218. unsigned int rec_fifo, indicator;
  219. unsigned char c;
  220. //printk("<0> irq=%d\n",irq);
  221. if (kfifo_is_full(&uartb_fifo)) {
  222. printk("<0> [UARTB] kfifo is full!!\n");
  223. *(volatile unsigned int *) 0xbe0d0110 = *(volatile unsigned int *) 0xbe0d0110; //w1c
  224. return IRQ_HANDLED;
  225. }
  226. rec_fifo = *(volatile unsigned int *) 0xbe0d0100; /* plz read 000, then 00c , hw limit, Gaia*/
  227. indicator = (*(volatile unsigned int *) 0xbe0d010c) & 0xf;
  228. for(i=0; indicator!=0; i++){
  229. indicator = indicator >>1;
  230. c = rec_fifo >> (i*8);
  231. if (kfifo_is_full(&uartb_fifo) == 0)
  232. kfifo_in(&uartb_fifo, &c, sizeof(unsigned char));
  233. else
  234. break;
  235. }
  236. //printk("<0> len = %d\n",kfifo_len(&uartb_fifo));
  237. *(volatile unsigned int *) 0xbe0d0110 = *(volatile unsigned int *) 0xbe0d0110; //w1c
  238. return IRQ_HANDLED;
  239. }
  240. static void uartb_dispatch(void)
  241. {
  242. do_IRQ(IRQ_UARTB);
  243. }
  244. static struct file_operations uartb_fops = {
  245. owner : THIS_MODULE,
  246. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,8))
  247. unlocked_ioctl : uartb_ioctl,
  248. #else
  249. ioctl : uartb_ioctl,
  250. #endif
  251. open : uartb_open,
  252. release : uartb_close,
  253. write : uartb_write,
  254. read : uartb_read,
  255. };
  256. /*
  257. static struct file_operations uartb_fops = {
  258. .ioctl = uartb_ioctl,
  259. .write = uartb_write,
  260. .open = uartb_open,
  261. .release = uartb_close,
  262. .owner = THIS_MODULE,
  263. };
  264. */
  265. struct cdev *cdev_uartb;
  266. int uartb_init(void)
  267. {
  268. int ret,err;
  269. dev_t devno = MKDEV(UARTB_MAJOR, 0);
  270. err=register_chrdev_region(devno, 1, "uartb");
  271. if(err)
  272. {
  273. return -EIO;
  274. }
  275. cdev_uartb = cdev_alloc();
  276. cdev_uartb->owner = THIS_MODULE;
  277. cdev_uartb->ops = &uartb_fops;
  278. err=cdev_add(cdev_uartb, devno, 1);
  279. if(err)
  280. {
  281. return -EIO;
  282. }
  283. set_vi_handler(IRQ_UARTB, uartb_dispatch);
  284. ret = request_irq(IRQ_UARTB, &uartb_ISR, IRQF_DISABLED, "uartb", NULL);
  285. *(volatile unsigned short *) 0xbe0d0114 = 0x666;//baud rate:115200,0x00000666:9600
  286. *(volatile unsigned int *) 0xbe0d0128 = 0;
  287. *(volatile unsigned int *) 0xbe0d0108 = 0x00200307;
  288. *(volatile unsigned int *) 0xbe0d0110 = *(volatile unsigned int *) 0xbe0d0110;
  289. while (*(volatile unsigned char *)0xbe0d010c)
  290. ret = (int) *(volatile unsigned int *)0xbe0d0100;
  291. ret = kfifo_alloc(&uartb_fifo, PAGE_SIZE, GFP_KERNEL);
  292. if (ret < 0)
  293. printk("<0>[UARTB] kfifo alloc error!");
  294. return ret;
  295. }
  296. #ifdef CONFIG_SUPPORT_UARTB
  297. module_init(uartb_init);
  298. #endif
  299. //MODULE_LICENSE("Dual BSD/GPL");