mfd.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /*
  2. * mfd.c: driver for High Speed UART device of Intel Medfield platform
  3. *
  4. * Refer pxa.c, 8250.c and some other drivers in drivers/serial/
  5. *
  6. * (C) Copyright 2010 Intel Corporation
  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
  10. * as published by the Free Software Foundation; version 2
  11. * of the License.
  12. */
  13. /* Notes:
  14. * 1. DMA channel allocation: 0/1 channel are assigned to port 0,
  15. * 2/3 chan to port 1, 4/5 chan to port 3. Even number chans
  16. * are used for RX, odd chans for TX
  17. *
  18. * 2. In A0 stepping, UART will not support TX half empty flag
  19. *
  20. * 3. The RI/DSR/DCD/DTR are not pinned out, DCD & DSR are always
  21. * asserted, only when the HW is reset the DDCD and DDSR will
  22. * be triggered
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/console.h>
  27. #include <linux/sysrq.h>
  28. #include <linux/slab.h>
  29. #include <linux/serial_reg.h>
  30. #include <linux/circ_buf.h>
  31. #include <linux/delay.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/tty.h>
  34. #include <linux/tty_flip.h>
  35. #include <linux/serial_core.h>
  36. #include <linux/serial_mfd.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/pci.h>
  39. #include <linux/io.h>
  40. #include <linux/debugfs.h>
  41. #define MFD_HSU_A0_STEPPING 1
  42. #define HSU_DMA_BUF_SIZE 2048
  43. #define chan_readl(chan, offset) readl(chan->reg + offset)
  44. #define chan_writel(chan, offset, val) writel(val, chan->reg + offset)
  45. #define mfd_readl(obj, offset) readl(obj->reg + offset)
  46. #define mfd_writel(obj, offset, val) writel(val, obj->reg + offset)
  47. #define HSU_DMA_TIMEOUT_CHECK_FREQ (HZ/10)
  48. struct hsu_dma_buffer {
  49. u8 *buf;
  50. dma_addr_t dma_addr;
  51. u32 dma_size;
  52. u32 ofs;
  53. };
  54. struct hsu_dma_chan {
  55. u32 id;
  56. enum dma_data_direction dirt;
  57. struct uart_hsu_port *uport;
  58. void __iomem *reg;
  59. struct timer_list rx_timer; /* only needed by RX channel */
  60. };
  61. struct uart_hsu_port {
  62. struct uart_port port;
  63. unsigned char ier;
  64. unsigned char lcr;
  65. unsigned char mcr;
  66. unsigned int lsr_break_flag;
  67. char name[12];
  68. int index;
  69. struct device *dev;
  70. struct hsu_dma_chan *txc;
  71. struct hsu_dma_chan *rxc;
  72. struct hsu_dma_buffer txbuf;
  73. struct hsu_dma_buffer rxbuf;
  74. int use_dma; /* flag for DMA/PIO */
  75. int running;
  76. int dma_tx_on;
  77. };
  78. /* Top level data structure of HSU */
  79. struct hsu_port {
  80. void __iomem *reg;
  81. unsigned long paddr;
  82. unsigned long iolen;
  83. u32 irq;
  84. struct uart_hsu_port port[3];
  85. struct hsu_dma_chan chans[10];
  86. struct dentry *debugfs;
  87. };
  88. static inline unsigned int serial_in(struct uart_hsu_port *up, int offset)
  89. {
  90. unsigned int val;
  91. if (offset > UART_MSR) {
  92. offset <<= 2;
  93. val = readl(up->port.membase + offset);
  94. } else
  95. val = (unsigned int)readb(up->port.membase + offset);
  96. return val;
  97. }
  98. static inline void serial_out(struct uart_hsu_port *up, int offset, int value)
  99. {
  100. if (offset > UART_MSR) {
  101. offset <<= 2;
  102. writel(value, up->port.membase + offset);
  103. } else {
  104. unsigned char val = value & 0xff;
  105. writeb(val, up->port.membase + offset);
  106. }
  107. }
  108. #ifdef CONFIG_DEBUG_FS
  109. #define HSU_REGS_BUFSIZE 1024
  110. static int hsu_show_regs_open(struct inode *inode, struct file *file)
  111. {
  112. file->private_data = inode->i_private;
  113. return 0;
  114. }
  115. static ssize_t port_show_regs(struct file *file, char __user *user_buf,
  116. size_t count, loff_t *ppos)
  117. {
  118. struct uart_hsu_port *up = file->private_data;
  119. char *buf;
  120. u32 len = 0;
  121. ssize_t ret;
  122. buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
  123. if (!buf)
  124. return 0;
  125. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  126. "MFD HSU port[%d] regs:\n", up->index);
  127. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  128. "=================================\n");
  129. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  130. "IER: \t\t0x%08x\n", serial_in(up, UART_IER));
  131. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  132. "IIR: \t\t0x%08x\n", serial_in(up, UART_IIR));
  133. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  134. "LCR: \t\t0x%08x\n", serial_in(up, UART_LCR));
  135. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  136. "MCR: \t\t0x%08x\n", serial_in(up, UART_MCR));
  137. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  138. "LSR: \t\t0x%08x\n", serial_in(up, UART_LSR));
  139. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  140. "MSR: \t\t0x%08x\n", serial_in(up, UART_MSR));
  141. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  142. "FOR: \t\t0x%08x\n", serial_in(up, UART_FOR));
  143. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  144. "PS: \t\t0x%08x\n", serial_in(up, UART_PS));
  145. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  146. "MUL: \t\t0x%08x\n", serial_in(up, UART_MUL));
  147. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  148. "DIV: \t\t0x%08x\n", serial_in(up, UART_DIV));
  149. if (len > HSU_REGS_BUFSIZE)
  150. len = HSU_REGS_BUFSIZE;
  151. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  152. kfree(buf);
  153. return ret;
  154. }
  155. static ssize_t dma_show_regs(struct file *file, char __user *user_buf,
  156. size_t count, loff_t *ppos)
  157. {
  158. struct hsu_dma_chan *chan = file->private_data;
  159. char *buf;
  160. u32 len = 0;
  161. ssize_t ret;
  162. buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
  163. if (!buf)
  164. return 0;
  165. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  166. "MFD HSU DMA channel [%d] regs:\n", chan->id);
  167. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  168. "=================================\n");
  169. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  170. "CR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_CR));
  171. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  172. "DCR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_DCR));
  173. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  174. "BSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_BSR));
  175. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  176. "MOTSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_MOTSR));
  177. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  178. "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0SAR));
  179. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  180. "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0TSR));
  181. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  182. "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1SAR));
  183. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  184. "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1TSR));
  185. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  186. "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2SAR));
  187. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  188. "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2TSR));
  189. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  190. "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3SAR));
  191. len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
  192. "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3TSR));
  193. if (len > HSU_REGS_BUFSIZE)
  194. len = HSU_REGS_BUFSIZE;
  195. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  196. kfree(buf);
  197. return ret;
  198. }
  199. static const struct file_operations port_regs_ops = {
  200. .owner = THIS_MODULE,
  201. .open = hsu_show_regs_open,
  202. .read = port_show_regs,
  203. .llseek = default_llseek,
  204. };
  205. static const struct file_operations dma_regs_ops = {
  206. .owner = THIS_MODULE,
  207. .open = hsu_show_regs_open,
  208. .read = dma_show_regs,
  209. .llseek = default_llseek,
  210. };
  211. static int hsu_debugfs_init(struct hsu_port *hsu)
  212. {
  213. int i;
  214. char name[32];
  215. hsu->debugfs = debugfs_create_dir("hsu", NULL);
  216. if (!hsu->debugfs)
  217. return -ENOMEM;
  218. for (i = 0; i < 3; i++) {
  219. snprintf(name, sizeof(name), "port_%d_regs", i);
  220. debugfs_create_file(name, S_IFREG | S_IRUGO,
  221. hsu->debugfs, (void *)(&hsu->port[i]), &port_regs_ops);
  222. }
  223. for (i = 0; i < 6; i++) {
  224. snprintf(name, sizeof(name), "dma_chan_%d_regs", i);
  225. debugfs_create_file(name, S_IFREG | S_IRUGO,
  226. hsu->debugfs, (void *)&hsu->chans[i], &dma_regs_ops);
  227. }
  228. return 0;
  229. }
  230. static void hsu_debugfs_remove(struct hsu_port *hsu)
  231. {
  232. if (hsu->debugfs)
  233. debugfs_remove_recursive(hsu->debugfs);
  234. }
  235. #else
  236. static inline int hsu_debugfs_init(struct hsu_port *hsu)
  237. {
  238. return 0;
  239. }
  240. static inline void hsu_debugfs_remove(struct hsu_port *hsu)
  241. {
  242. }
  243. #endif /* CONFIG_DEBUG_FS */
  244. static void serial_hsu_enable_ms(struct uart_port *port)
  245. {
  246. struct uart_hsu_port *up =
  247. container_of(port, struct uart_hsu_port, port);
  248. up->ier |= UART_IER_MSI;
  249. serial_out(up, UART_IER, up->ier);
  250. }
  251. void hsu_dma_tx(struct uart_hsu_port *up)
  252. {
  253. struct circ_buf *xmit = &up->port.state->xmit;
  254. struct hsu_dma_buffer *dbuf = &up->txbuf;
  255. int count;
  256. /* test_and_set_bit may be better, but anyway it's in lock protected mode */
  257. if (up->dma_tx_on)
  258. return;
  259. /* Update the circ buf info */
  260. xmit->tail += dbuf->ofs;
  261. xmit->tail &= UART_XMIT_SIZE - 1;
  262. up->port.icount.tx += dbuf->ofs;
  263. dbuf->ofs = 0;
  264. /* Disable the channel */
  265. chan_writel(up->txc, HSU_CH_CR, 0x0);
  266. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&up->port)) {
  267. dma_sync_single_for_device(up->port.dev,
  268. dbuf->dma_addr,
  269. dbuf->dma_size,
  270. DMA_TO_DEVICE);
  271. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  272. dbuf->ofs = count;
  273. /* Reprogram the channel */
  274. chan_writel(up->txc, HSU_CH_D0SAR, dbuf->dma_addr + xmit->tail);
  275. chan_writel(up->txc, HSU_CH_D0TSR, count);
  276. /* Reenable the channel */
  277. chan_writel(up->txc, HSU_CH_DCR, 0x1
  278. | (0x1 << 8)
  279. | (0x1 << 16)
  280. | (0x1 << 24));
  281. up->dma_tx_on = 1;
  282. chan_writel(up->txc, HSU_CH_CR, 0x1);
  283. }
  284. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  285. uart_write_wakeup(&up->port);
  286. }
  287. /* The buffer is already cache coherent */
  288. void hsu_dma_start_rx_chan(struct hsu_dma_chan *rxc, struct hsu_dma_buffer *dbuf)
  289. {
  290. dbuf->ofs = 0;
  291. chan_writel(rxc, HSU_CH_BSR, 32);
  292. chan_writel(rxc, HSU_CH_MOTSR, 4);
  293. chan_writel(rxc, HSU_CH_D0SAR, dbuf->dma_addr);
  294. chan_writel(rxc, HSU_CH_D0TSR, dbuf->dma_size);
  295. chan_writel(rxc, HSU_CH_DCR, 0x1 | (0x1 << 8)
  296. | (0x1 << 16)
  297. | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
  298. );
  299. chan_writel(rxc, HSU_CH_CR, 0x3);
  300. mod_timer(&rxc->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
  301. }
  302. /* Protected by spin_lock_irqsave(port->lock) */
  303. static void serial_hsu_start_tx(struct uart_port *port)
  304. {
  305. struct uart_hsu_port *up =
  306. container_of(port, struct uart_hsu_port, port);
  307. if (up->use_dma) {
  308. hsu_dma_tx(up);
  309. } else if (!(up->ier & UART_IER_THRI)) {
  310. up->ier |= UART_IER_THRI;
  311. serial_out(up, UART_IER, up->ier);
  312. }
  313. }
  314. static void serial_hsu_stop_tx(struct uart_port *port)
  315. {
  316. struct uart_hsu_port *up =
  317. container_of(port, struct uart_hsu_port, port);
  318. struct hsu_dma_chan *txc = up->txc;
  319. if (up->use_dma)
  320. chan_writel(txc, HSU_CH_CR, 0x0);
  321. else if (up->ier & UART_IER_THRI) {
  322. up->ier &= ~UART_IER_THRI;
  323. serial_out(up, UART_IER, up->ier);
  324. }
  325. }
  326. /* This is always called in spinlock protected mode, so
  327. * modify timeout timer is safe here */
  328. void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
  329. {
  330. struct hsu_dma_buffer *dbuf = &up->rxbuf;
  331. struct hsu_dma_chan *chan = up->rxc;
  332. struct uart_port *port = &up->port;
  333. struct tty_struct *tty = port->state->port.tty;
  334. int count;
  335. if (!tty)
  336. return;
  337. /*
  338. * First need to know how many is already transferred,
  339. * then check if its a timeout DMA irq, and return
  340. * the trail bytes out, push them up and reenable the
  341. * channel
  342. */
  343. /* Timeout IRQ, need wait some time, see Errata 2 */
  344. if (int_sts & 0xf00)
  345. udelay(2);
  346. /* Stop the channel */
  347. chan_writel(chan, HSU_CH_CR, 0x0);
  348. count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
  349. if (!count) {
  350. /* Restart the channel before we leave */
  351. chan_writel(chan, HSU_CH_CR, 0x3);
  352. return;
  353. }
  354. del_timer(&chan->rx_timer);
  355. dma_sync_single_for_cpu(port->dev, dbuf->dma_addr,
  356. dbuf->dma_size, DMA_FROM_DEVICE);
  357. /*
  358. * Head will only wrap around when we recycle
  359. * the DMA buffer, and when that happens, we
  360. * explicitly set tail to 0. So head will
  361. * always be greater than tail.
  362. */
  363. tty_insert_flip_string(tty, dbuf->buf, count);
  364. port->icount.rx += count;
  365. dma_sync_single_for_device(up->port.dev, dbuf->dma_addr,
  366. dbuf->dma_size, DMA_FROM_DEVICE);
  367. /* Reprogram the channel */
  368. chan_writel(chan, HSU_CH_D0SAR, dbuf->dma_addr);
  369. chan_writel(chan, HSU_CH_D0TSR, dbuf->dma_size);
  370. chan_writel(chan, HSU_CH_DCR, 0x1
  371. | (0x1 << 8)
  372. | (0x1 << 16)
  373. | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
  374. );
  375. tty_flip_buffer_push(tty);
  376. chan_writel(chan, HSU_CH_CR, 0x3);
  377. chan->rx_timer.expires = jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ;
  378. add_timer(&chan->rx_timer);
  379. }
  380. static void serial_hsu_stop_rx(struct uart_port *port)
  381. {
  382. struct uart_hsu_port *up =
  383. container_of(port, struct uart_hsu_port, port);
  384. struct hsu_dma_chan *chan = up->rxc;
  385. if (up->use_dma)
  386. chan_writel(chan, HSU_CH_CR, 0x2);
  387. else {
  388. up->ier &= ~UART_IER_RLSI;
  389. up->port.read_status_mask &= ~UART_LSR_DR;
  390. serial_out(up, UART_IER, up->ier);
  391. }
  392. }
  393. static inline void receive_chars(struct uart_hsu_port *up, int *status)
  394. {
  395. struct tty_struct *tty = up->port.state->port.tty;
  396. unsigned int ch, flag;
  397. unsigned int max_count = 256;
  398. if (!tty)
  399. return;
  400. do {
  401. ch = serial_in(up, UART_RX);
  402. flag = TTY_NORMAL;
  403. up->port.icount.rx++;
  404. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  405. UART_LSR_FE | UART_LSR_OE))) {
  406. dev_warn(up->dev, "We really rush into ERR/BI case"
  407. "status = 0x%02x", *status);
  408. /* For statistics only */
  409. if (*status & UART_LSR_BI) {
  410. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  411. up->port.icount.brk++;
  412. /*
  413. * We do the SysRQ and SAK checking
  414. * here because otherwise the break
  415. * may get masked by ignore_status_mask
  416. * or read_status_mask.
  417. */
  418. if (uart_handle_break(&up->port))
  419. goto ignore_char;
  420. } else if (*status & UART_LSR_PE)
  421. up->port.icount.parity++;
  422. else if (*status & UART_LSR_FE)
  423. up->port.icount.frame++;
  424. if (*status & UART_LSR_OE)
  425. up->port.icount.overrun++;
  426. /* Mask off conditions which should be ignored. */
  427. *status &= up->port.read_status_mask;
  428. #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
  429. if (up->port.cons &&
  430. up->port.cons->index == up->port.line) {
  431. /* Recover the break flag from console xmit */
  432. *status |= up->lsr_break_flag;
  433. up->lsr_break_flag = 0;
  434. }
  435. #endif
  436. if (*status & UART_LSR_BI) {
  437. flag = TTY_BREAK;
  438. } else if (*status & UART_LSR_PE)
  439. flag = TTY_PARITY;
  440. else if (*status & UART_LSR_FE)
  441. flag = TTY_FRAME;
  442. }
  443. if (uart_handle_sysrq_char(&up->port, ch))
  444. goto ignore_char;
  445. uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
  446. ignore_char:
  447. *status = serial_in(up, UART_LSR);
  448. } while ((*status & UART_LSR_DR) && max_count--);
  449. tty_flip_buffer_push(tty);
  450. }
  451. static void transmit_chars(struct uart_hsu_port *up)
  452. {
  453. struct circ_buf *xmit = &up->port.state->xmit;
  454. int count;
  455. if (up->port.x_char) {
  456. serial_out(up, UART_TX, up->port.x_char);
  457. up->port.icount.tx++;
  458. up->port.x_char = 0;
  459. return;
  460. }
  461. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  462. serial_hsu_stop_tx(&up->port);
  463. return;
  464. }
  465. #ifndef MFD_HSU_A0_STEPPING
  466. count = up->port.fifosize / 2;
  467. #else
  468. /*
  469. * A0 only supports fully empty IRQ, and the first char written
  470. * into it won't clear the EMPT bit, so we may need be cautious
  471. * by useing a shorter buffer
  472. */
  473. count = up->port.fifosize - 4;
  474. #endif
  475. do {
  476. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  477. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  478. up->port.icount.tx++;
  479. if (uart_circ_empty(xmit))
  480. break;
  481. } while (--count > 0);
  482. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  483. uart_write_wakeup(&up->port);
  484. if (uart_circ_empty(xmit))
  485. serial_hsu_stop_tx(&up->port);
  486. }
  487. static inline void check_modem_status(struct uart_hsu_port *up)
  488. {
  489. int status;
  490. status = serial_in(up, UART_MSR);
  491. if ((status & UART_MSR_ANY_DELTA) == 0)
  492. return;
  493. if (status & UART_MSR_TERI)
  494. up->port.icount.rng++;
  495. if (status & UART_MSR_DDSR)
  496. up->port.icount.dsr++;
  497. /* We may only get DDCD when HW init and reset */
  498. if (status & UART_MSR_DDCD)
  499. uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
  500. /* Will start/stop_tx accordingly */
  501. if (status & UART_MSR_DCTS)
  502. uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
  503. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  504. }
  505. /*
  506. * This handles the interrupt from one port.
  507. */
  508. static irqreturn_t port_irq(int irq, void *dev_id)
  509. {
  510. struct uart_hsu_port *up = dev_id;
  511. unsigned int iir, lsr;
  512. unsigned long flags;
  513. if (unlikely(!up->running))
  514. return IRQ_NONE;
  515. spin_lock_irqsave(&up->port.lock, flags);
  516. if (up->use_dma) {
  517. lsr = serial_in(up, UART_LSR);
  518. if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
  519. UART_LSR_FE | UART_LSR_OE)))
  520. dev_warn(up->dev,
  521. "Got lsr irq while using DMA, lsr = 0x%2x\n",
  522. lsr);
  523. check_modem_status(up);
  524. spin_unlock_irqrestore(&up->port.lock, flags);
  525. return IRQ_HANDLED;
  526. }
  527. iir = serial_in(up, UART_IIR);
  528. if (iir & UART_IIR_NO_INT) {
  529. spin_unlock_irqrestore(&up->port.lock, flags);
  530. return IRQ_NONE;
  531. }
  532. lsr = serial_in(up, UART_LSR);
  533. if (lsr & UART_LSR_DR)
  534. receive_chars(up, &lsr);
  535. check_modem_status(up);
  536. /* lsr will be renewed during the receive_chars */
  537. if (lsr & UART_LSR_THRE)
  538. transmit_chars(up);
  539. spin_unlock_irqrestore(&up->port.lock, flags);
  540. return IRQ_HANDLED;
  541. }
  542. static inline void dma_chan_irq(struct hsu_dma_chan *chan)
  543. {
  544. struct uart_hsu_port *up = chan->uport;
  545. unsigned long flags;
  546. u32 int_sts;
  547. spin_lock_irqsave(&up->port.lock, flags);
  548. if (!up->use_dma || !up->running)
  549. goto exit;
  550. /*
  551. * No matter what situation, need read clear the IRQ status
  552. * There is a bug, see Errata 5, HSD 2900918
  553. */
  554. int_sts = chan_readl(chan, HSU_CH_SR);
  555. /* Rx channel */
  556. if (chan->dirt == DMA_FROM_DEVICE)
  557. hsu_dma_rx(up, int_sts);
  558. /* Tx channel */
  559. if (chan->dirt == DMA_TO_DEVICE) {
  560. chan_writel(chan, HSU_CH_CR, 0x0);
  561. up->dma_tx_on = 0;
  562. hsu_dma_tx(up);
  563. }
  564. exit:
  565. spin_unlock_irqrestore(&up->port.lock, flags);
  566. return;
  567. }
  568. static irqreturn_t dma_irq(int irq, void *dev_id)
  569. {
  570. struct hsu_port *hsu = dev_id;
  571. u32 int_sts, i;
  572. int_sts = mfd_readl(hsu, HSU_GBL_DMAISR);
  573. /* Currently we only have 6 channels may be used */
  574. for (i = 0; i < 6; i++) {
  575. if (int_sts & 0x1)
  576. dma_chan_irq(&hsu->chans[i]);
  577. int_sts >>= 1;
  578. }
  579. return IRQ_HANDLED;
  580. }
  581. static unsigned int serial_hsu_tx_empty(struct uart_port *port)
  582. {
  583. struct uart_hsu_port *up =
  584. container_of(port, struct uart_hsu_port, port);
  585. unsigned long flags;
  586. unsigned int ret;
  587. spin_lock_irqsave(&up->port.lock, flags);
  588. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  589. spin_unlock_irqrestore(&up->port.lock, flags);
  590. return ret;
  591. }
  592. static unsigned int serial_hsu_get_mctrl(struct uart_port *port)
  593. {
  594. struct uart_hsu_port *up =
  595. container_of(port, struct uart_hsu_port, port);
  596. unsigned char status;
  597. unsigned int ret;
  598. status = serial_in(up, UART_MSR);
  599. ret = 0;
  600. if (status & UART_MSR_DCD)
  601. ret |= TIOCM_CAR;
  602. if (status & UART_MSR_RI)
  603. ret |= TIOCM_RNG;
  604. if (status & UART_MSR_DSR)
  605. ret |= TIOCM_DSR;
  606. if (status & UART_MSR_CTS)
  607. ret |= TIOCM_CTS;
  608. return ret;
  609. }
  610. static void serial_hsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
  611. {
  612. struct uart_hsu_port *up =
  613. container_of(port, struct uart_hsu_port, port);
  614. unsigned char mcr = 0;
  615. if (mctrl & TIOCM_RTS)
  616. mcr |= UART_MCR_RTS;
  617. if (mctrl & TIOCM_DTR)
  618. mcr |= UART_MCR_DTR;
  619. if (mctrl & TIOCM_OUT1)
  620. mcr |= UART_MCR_OUT1;
  621. if (mctrl & TIOCM_OUT2)
  622. mcr |= UART_MCR_OUT2;
  623. if (mctrl & TIOCM_LOOP)
  624. mcr |= UART_MCR_LOOP;
  625. mcr |= up->mcr;
  626. serial_out(up, UART_MCR, mcr);
  627. }
  628. static void serial_hsu_break_ctl(struct uart_port *port, int break_state)
  629. {
  630. struct uart_hsu_port *up =
  631. container_of(port, struct uart_hsu_port, port);
  632. unsigned long flags;
  633. spin_lock_irqsave(&up->port.lock, flags);
  634. if (break_state == -1)
  635. up->lcr |= UART_LCR_SBC;
  636. else
  637. up->lcr &= ~UART_LCR_SBC;
  638. serial_out(up, UART_LCR, up->lcr);
  639. spin_unlock_irqrestore(&up->port.lock, flags);
  640. }
  641. /*
  642. * What special to do:
  643. * 1. chose the 64B fifo mode
  644. * 2. make sure not to select half empty mode for A0 stepping
  645. * 3. start dma or pio depends on configuration
  646. * 4. we only allocate dma memory when needed
  647. */
  648. static int serial_hsu_startup(struct uart_port *port)
  649. {
  650. struct uart_hsu_port *up =
  651. container_of(port, struct uart_hsu_port, port);
  652. unsigned long flags;
  653. /*
  654. * Clear the FIFO buffers and disable them.
  655. * (they will be reenabled in set_termios())
  656. */
  657. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  658. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  659. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  660. serial_out(up, UART_FCR, 0);
  661. /* Clear the interrupt registers. */
  662. (void) serial_in(up, UART_LSR);
  663. (void) serial_in(up, UART_RX);
  664. (void) serial_in(up, UART_IIR);
  665. (void) serial_in(up, UART_MSR);
  666. /* Now, initialize the UART, default is 8n1 */
  667. serial_out(up, UART_LCR, UART_LCR_WLEN8);
  668. spin_lock_irqsave(&up->port.lock, flags);
  669. up->port.mctrl |= TIOCM_OUT2;
  670. serial_hsu_set_mctrl(&up->port, up->port.mctrl);
  671. /*
  672. * Finally, enable interrupts. Note: Modem status interrupts
  673. * are set via set_termios(), which will be occurring imminently
  674. * anyway, so we don't enable them here.
  675. */
  676. if (!up->use_dma)
  677. up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE;
  678. else
  679. up->ier = 0;
  680. serial_out(up, UART_IER, up->ier);
  681. spin_unlock_irqrestore(&up->port.lock, flags);
  682. /* DMA init */
  683. if (up->use_dma) {
  684. struct hsu_dma_buffer *dbuf;
  685. struct circ_buf *xmit = &port->state->xmit;
  686. up->dma_tx_on = 0;
  687. /* First allocate the RX buffer */
  688. dbuf = &up->rxbuf;
  689. dbuf->buf = kzalloc(HSU_DMA_BUF_SIZE, GFP_KERNEL);
  690. if (!dbuf->buf) {
  691. up->use_dma = 0;
  692. goto exit;
  693. }
  694. dbuf->dma_addr = dma_map_single(port->dev,
  695. dbuf->buf,
  696. HSU_DMA_BUF_SIZE,
  697. DMA_FROM_DEVICE);
  698. dbuf->dma_size = HSU_DMA_BUF_SIZE;
  699. /* Start the RX channel right now */
  700. hsu_dma_start_rx_chan(up->rxc, dbuf);
  701. /* Next init the TX DMA */
  702. dbuf = &up->txbuf;
  703. dbuf->buf = xmit->buf;
  704. dbuf->dma_addr = dma_map_single(port->dev,
  705. dbuf->buf,
  706. UART_XMIT_SIZE,
  707. DMA_TO_DEVICE);
  708. dbuf->dma_size = UART_XMIT_SIZE;
  709. /* This should not be changed all around */
  710. chan_writel(up->txc, HSU_CH_BSR, 32);
  711. chan_writel(up->txc, HSU_CH_MOTSR, 4);
  712. dbuf->ofs = 0;
  713. }
  714. exit:
  715. /* And clear the interrupt registers again for luck. */
  716. (void) serial_in(up, UART_LSR);
  717. (void) serial_in(up, UART_RX);
  718. (void) serial_in(up, UART_IIR);
  719. (void) serial_in(up, UART_MSR);
  720. up->running = 1;
  721. return 0;
  722. }
  723. static void serial_hsu_shutdown(struct uart_port *port)
  724. {
  725. struct uart_hsu_port *up =
  726. container_of(port, struct uart_hsu_port, port);
  727. unsigned long flags;
  728. del_timer_sync(&up->rxc->rx_timer);
  729. /* Disable interrupts from this port */
  730. up->ier = 0;
  731. serial_out(up, UART_IER, 0);
  732. up->running = 0;
  733. spin_lock_irqsave(&up->port.lock, flags);
  734. up->port.mctrl &= ~TIOCM_OUT2;
  735. serial_hsu_set_mctrl(&up->port, up->port.mctrl);
  736. spin_unlock_irqrestore(&up->port.lock, flags);
  737. /* Disable break condition and FIFOs */
  738. serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
  739. serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  740. UART_FCR_CLEAR_RCVR |
  741. UART_FCR_CLEAR_XMIT);
  742. serial_out(up, UART_FCR, 0);
  743. }
  744. static void
  745. serial_hsu_set_termios(struct uart_port *port, struct ktermios *termios,
  746. struct ktermios *old)
  747. {
  748. struct uart_hsu_port *up =
  749. container_of(port, struct uart_hsu_port, port);
  750. struct tty_struct *tty = port->state->port.tty;
  751. unsigned char cval, fcr = 0;
  752. unsigned long flags;
  753. unsigned int baud, quot;
  754. u32 ps, mul;
  755. switch (termios->c_cflag & CSIZE) {
  756. case CS5:
  757. cval = UART_LCR_WLEN5;
  758. break;
  759. case CS6:
  760. cval = UART_LCR_WLEN6;
  761. break;
  762. case CS7:
  763. cval = UART_LCR_WLEN7;
  764. break;
  765. default:
  766. case CS8:
  767. cval = UART_LCR_WLEN8;
  768. break;
  769. }
  770. /* CMSPAR isn't supported by this driver */
  771. if (tty)
  772. tty->termios->c_cflag &= ~CMSPAR;
  773. if (termios->c_cflag & CSTOPB)
  774. cval |= UART_LCR_STOP;
  775. if (termios->c_cflag & PARENB)
  776. cval |= UART_LCR_PARITY;
  777. if (!(termios->c_cflag & PARODD))
  778. cval |= UART_LCR_EPAR;
  779. /*
  780. * The base clk is 50Mhz, and the baud rate come from:
  781. * baud = 50M * MUL / (DIV * PS * DLAB)
  782. *
  783. * For those basic low baud rate we can get the direct
  784. * scalar from 2746800, like 115200 = 2746800/24. For those
  785. * higher baud rate, we handle them case by case, mainly by
  786. * adjusting the MUL/PS registers, and DIV register is kept
  787. * as default value 0x3d09 to make things simple
  788. */
  789. baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  790. quot = 1;
  791. ps = 0x10;
  792. mul = 0x3600;
  793. switch (baud) {
  794. case 3500000:
  795. mul = 0x3345;
  796. ps = 0xC;
  797. break;
  798. case 1843200:
  799. mul = 0x2400;
  800. break;
  801. case 3000000:
  802. case 2500000:
  803. case 2000000:
  804. case 1500000:
  805. case 1000000:
  806. case 500000:
  807. /* mul/ps/quot = 0x9C4/0x10/0x1 will make a 500000 bps */
  808. mul = baud / 500000 * 0x9C4;
  809. break;
  810. default:
  811. /* Use uart_get_divisor to get quot for other baud rates */
  812. quot = 0;
  813. }
  814. if (!quot)
  815. quot = uart_get_divisor(port, baud);
  816. if ((up->port.uartclk / quot) < (2400 * 16))
  817. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_1B;
  818. else if ((up->port.uartclk / quot) < (230400 * 16))
  819. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_16B;
  820. else
  821. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_32B;
  822. fcr |= UART_FCR_HSU_64B_FIFO;
  823. #ifdef MFD_HSU_A0_STEPPING
  824. /* A0 doesn't support half empty IRQ */
  825. fcr |= UART_FCR_FULL_EMPT_TXI;
  826. #endif
  827. /*
  828. * Ok, we're now changing the port state. Do it with
  829. * interrupts disabled.
  830. */
  831. spin_lock_irqsave(&up->port.lock, flags);
  832. /* Update the per-port timeout */
  833. uart_update_timeout(port, termios->c_cflag, baud);
  834. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  835. if (termios->c_iflag & INPCK)
  836. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  837. if (termios->c_iflag & (BRKINT | PARMRK))
  838. up->port.read_status_mask |= UART_LSR_BI;
  839. /* Characters to ignore */
  840. up->port.ignore_status_mask = 0;
  841. if (termios->c_iflag & IGNPAR)
  842. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  843. if (termios->c_iflag & IGNBRK) {
  844. up->port.ignore_status_mask |= UART_LSR_BI;
  845. /*
  846. * If we're ignoring parity and break indicators,
  847. * ignore overruns too (for real raw support).
  848. */
  849. if (termios->c_iflag & IGNPAR)
  850. up->port.ignore_status_mask |= UART_LSR_OE;
  851. }
  852. /* Ignore all characters if CREAD is not set */
  853. if ((termios->c_cflag & CREAD) == 0)
  854. up->port.ignore_status_mask |= UART_LSR_DR;
  855. /*
  856. * CTS flow control flag and modem status interrupts, disable
  857. * MSI by default
  858. */
  859. up->ier &= ~UART_IER_MSI;
  860. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  861. up->ier |= UART_IER_MSI;
  862. serial_out(up, UART_IER, up->ier);
  863. if (termios->c_cflag & CRTSCTS)
  864. up->mcr |= UART_MCR_AFE | UART_MCR_RTS;
  865. else
  866. up->mcr &= ~UART_MCR_AFE;
  867. serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
  868. serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
  869. serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
  870. serial_out(up, UART_LCR, cval); /* reset DLAB */
  871. serial_out(up, UART_MUL, mul); /* set MUL */
  872. serial_out(up, UART_PS, ps); /* set PS */
  873. up->lcr = cval; /* Save LCR */
  874. serial_hsu_set_mctrl(&up->port, up->port.mctrl);
  875. serial_out(up, UART_FCR, fcr);
  876. spin_unlock_irqrestore(&up->port.lock, flags);
  877. }
  878. static void
  879. serial_hsu_pm(struct uart_port *port, unsigned int state,
  880. unsigned int oldstate)
  881. {
  882. }
  883. static void serial_hsu_release_port(struct uart_port *port)
  884. {
  885. }
  886. static int serial_hsu_request_port(struct uart_port *port)
  887. {
  888. return 0;
  889. }
  890. static void serial_hsu_config_port(struct uart_port *port, int flags)
  891. {
  892. struct uart_hsu_port *up =
  893. container_of(port, struct uart_hsu_port, port);
  894. up->port.type = PORT_MFD;
  895. }
  896. static int
  897. serial_hsu_verify_port(struct uart_port *port, struct serial_struct *ser)
  898. {
  899. /* We don't want the core code to modify any port params */
  900. return -EINVAL;
  901. }
  902. static const char *
  903. serial_hsu_type(struct uart_port *port)
  904. {
  905. struct uart_hsu_port *up =
  906. container_of(port, struct uart_hsu_port, port);
  907. return up->name;
  908. }
  909. /* Mainly for uart console use */
  910. static struct uart_hsu_port *serial_hsu_ports[3];
  911. static struct uart_driver serial_hsu_reg;
  912. #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
  913. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  914. /* Wait for transmitter & holding register to empty */
  915. static inline void wait_for_xmitr(struct uart_hsu_port *up)
  916. {
  917. unsigned int status, tmout = 1000;
  918. /* Wait up to 1ms for the character to be sent. */
  919. do {
  920. status = serial_in(up, UART_LSR);
  921. if (status & UART_LSR_BI)
  922. up->lsr_break_flag = UART_LSR_BI;
  923. if (--tmout == 0)
  924. break;
  925. udelay(1);
  926. } while (!(status & BOTH_EMPTY));
  927. /* Wait up to 1s for flow control if necessary */
  928. if (up->port.flags & UPF_CONS_FLOW) {
  929. tmout = 1000000;
  930. while (--tmout &&
  931. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  932. udelay(1);
  933. }
  934. }
  935. static void serial_hsu_console_putchar(struct uart_port *port, int ch)
  936. {
  937. struct uart_hsu_port *up =
  938. container_of(port, struct uart_hsu_port, port);
  939. wait_for_xmitr(up);
  940. serial_out(up, UART_TX, ch);
  941. }
  942. /*
  943. * Print a string to the serial port trying not to disturb
  944. * any possible real use of the port...
  945. *
  946. * The console_lock must be held when we get here.
  947. */
  948. static void
  949. serial_hsu_console_write(struct console *co, const char *s, unsigned int count)
  950. {
  951. struct uart_hsu_port *up = serial_hsu_ports[co->index];
  952. unsigned long flags;
  953. unsigned int ier;
  954. int locked = 1;
  955. local_irq_save(flags);
  956. if (up->port.sysrq)
  957. locked = 0;
  958. else if (oops_in_progress) {
  959. locked = spin_trylock(&up->port.lock);
  960. } else
  961. spin_lock(&up->port.lock);
  962. /* First save the IER then disable the interrupts */
  963. ier = serial_in(up, UART_IER);
  964. serial_out(up, UART_IER, 0);
  965. uart_console_write(&up->port, s, count, serial_hsu_console_putchar);
  966. /*
  967. * Finally, wait for transmitter to become empty
  968. * and restore the IER
  969. */
  970. wait_for_xmitr(up);
  971. serial_out(up, UART_IER, ier);
  972. if (locked)
  973. spin_unlock(&up->port.lock);
  974. local_irq_restore(flags);
  975. }
  976. static struct console serial_hsu_console;
  977. static int __init
  978. serial_hsu_console_setup(struct console *co, char *options)
  979. {
  980. struct uart_hsu_port *up;
  981. int baud = 115200;
  982. int bits = 8;
  983. int parity = 'n';
  984. int flow = 'n';
  985. int ret;
  986. if (co->index == -1 || co->index >= serial_hsu_reg.nr)
  987. co->index = 0;
  988. up = serial_hsu_ports[co->index];
  989. if (!up)
  990. return -ENODEV;
  991. if (options)
  992. uart_parse_options(options, &baud, &parity, &bits, &flow);
  993. ret = uart_set_options(&up->port, co, baud, parity, bits, flow);
  994. return ret;
  995. }
  996. static struct console serial_hsu_console = {
  997. .name = "ttyMFD",
  998. .write = serial_hsu_console_write,
  999. .device = uart_console_device,
  1000. .setup = serial_hsu_console_setup,
  1001. .flags = CON_PRINTBUFFER,
  1002. .index = 2,
  1003. .data = &serial_hsu_reg,
  1004. };
  1005. #endif
  1006. struct uart_ops serial_hsu_pops = {
  1007. .tx_empty = serial_hsu_tx_empty,
  1008. .set_mctrl = serial_hsu_set_mctrl,
  1009. .get_mctrl = serial_hsu_get_mctrl,
  1010. .stop_tx = serial_hsu_stop_tx,
  1011. .start_tx = serial_hsu_start_tx,
  1012. .stop_rx = serial_hsu_stop_rx,
  1013. .enable_ms = serial_hsu_enable_ms,
  1014. .break_ctl = serial_hsu_break_ctl,
  1015. .startup = serial_hsu_startup,
  1016. .shutdown = serial_hsu_shutdown,
  1017. .set_termios = serial_hsu_set_termios,
  1018. .pm = serial_hsu_pm,
  1019. .type = serial_hsu_type,
  1020. .release_port = serial_hsu_release_port,
  1021. .request_port = serial_hsu_request_port,
  1022. .config_port = serial_hsu_config_port,
  1023. .verify_port = serial_hsu_verify_port,
  1024. };
  1025. static struct uart_driver serial_hsu_reg = {
  1026. .owner = THIS_MODULE,
  1027. .driver_name = "MFD serial",
  1028. .dev_name = "ttyMFD",
  1029. .major = TTY_MAJOR,
  1030. .minor = 128,
  1031. .nr = 3,
  1032. };
  1033. #ifdef CONFIG_PM
  1034. static int serial_hsu_suspend(struct pci_dev *pdev, pm_message_t state)
  1035. {
  1036. void *priv = pci_get_drvdata(pdev);
  1037. struct uart_hsu_port *up;
  1038. /* Make sure this is not the internal dma controller */
  1039. if (priv && (pdev->device != 0x081E)) {
  1040. up = priv;
  1041. uart_suspend_port(&serial_hsu_reg, &up->port);
  1042. }
  1043. pci_save_state(pdev);
  1044. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1045. return 0;
  1046. }
  1047. static int serial_hsu_resume(struct pci_dev *pdev)
  1048. {
  1049. void *priv = pci_get_drvdata(pdev);
  1050. struct uart_hsu_port *up;
  1051. int ret;
  1052. pci_set_power_state(pdev, PCI_D0);
  1053. pci_restore_state(pdev);
  1054. ret = pci_enable_device(pdev);
  1055. if (ret)
  1056. dev_warn(&pdev->dev,
  1057. "HSU: can't re-enable device, try to continue\n");
  1058. if (priv && (pdev->device != 0x081E)) {
  1059. up = priv;
  1060. uart_resume_port(&serial_hsu_reg, &up->port);
  1061. }
  1062. return 0;
  1063. }
  1064. #else
  1065. #define serial_hsu_suspend NULL
  1066. #define serial_hsu_resume NULL
  1067. #endif
  1068. /* temp global pointer before we settle down on using one or four PCI dev */
  1069. static struct hsu_port *phsu;
  1070. static int serial_hsu_probe(struct pci_dev *pdev,
  1071. const struct pci_device_id *ent)
  1072. {
  1073. struct uart_hsu_port *uport;
  1074. int index, ret;
  1075. printk(KERN_INFO "HSU: found PCI Serial controller(ID: %04x:%04x)\n",
  1076. pdev->vendor, pdev->device);
  1077. switch (pdev->device) {
  1078. case 0x081B:
  1079. index = 0;
  1080. break;
  1081. case 0x081C:
  1082. index = 1;
  1083. break;
  1084. case 0x081D:
  1085. index = 2;
  1086. break;
  1087. case 0x081E:
  1088. /* internal DMA controller */
  1089. index = 3;
  1090. break;
  1091. default:
  1092. dev_err(&pdev->dev, "HSU: out of index!");
  1093. return -ENODEV;
  1094. }
  1095. ret = pci_enable_device(pdev);
  1096. if (ret)
  1097. return ret;
  1098. if (index == 3) {
  1099. /* DMA controller */
  1100. ret = request_irq(pdev->irq, dma_irq, 0, "hsu_dma", phsu);
  1101. if (ret) {
  1102. dev_err(&pdev->dev, "can not get IRQ\n");
  1103. goto err_disable;
  1104. }
  1105. pci_set_drvdata(pdev, phsu);
  1106. } else {
  1107. /* UART port 0~2 */
  1108. uport = &phsu->port[index];
  1109. uport->port.irq = pdev->irq;
  1110. uport->port.dev = &pdev->dev;
  1111. uport->dev = &pdev->dev;
  1112. ret = request_irq(pdev->irq, port_irq, 0, uport->name, uport);
  1113. if (ret) {
  1114. dev_err(&pdev->dev, "can not get IRQ\n");
  1115. goto err_disable;
  1116. }
  1117. uart_add_one_port(&serial_hsu_reg, &uport->port);
  1118. #ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
  1119. if (index == 2) {
  1120. register_console(&serial_hsu_console);
  1121. uport->port.cons = &serial_hsu_console;
  1122. }
  1123. #endif
  1124. pci_set_drvdata(pdev, uport);
  1125. }
  1126. return 0;
  1127. err_disable:
  1128. pci_disable_device(pdev);
  1129. return ret;
  1130. }
  1131. static void hsu_dma_rx_timeout(unsigned long data)
  1132. {
  1133. struct hsu_dma_chan *chan = (void *)data;
  1134. struct uart_hsu_port *up = chan->uport;
  1135. struct hsu_dma_buffer *dbuf = &up->rxbuf;
  1136. int count = 0;
  1137. unsigned long flags;
  1138. spin_lock_irqsave(&up->port.lock, flags);
  1139. count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
  1140. if (!count) {
  1141. mod_timer(&chan->rx_timer, jiffies + HSU_DMA_TIMEOUT_CHECK_FREQ);
  1142. goto exit;
  1143. }
  1144. hsu_dma_rx(up, 0);
  1145. exit:
  1146. spin_unlock_irqrestore(&up->port.lock, flags);
  1147. }
  1148. static void hsu_global_init(void)
  1149. {
  1150. struct hsu_port *hsu;
  1151. struct uart_hsu_port *uport;
  1152. struct hsu_dma_chan *dchan;
  1153. int i, ret;
  1154. hsu = kzalloc(sizeof(struct hsu_port), GFP_KERNEL);
  1155. if (!hsu)
  1156. return;
  1157. /* Get basic io resource and map it */
  1158. hsu->paddr = 0xffa28000;
  1159. hsu->iolen = 0x1000;
  1160. if (!(request_mem_region(hsu->paddr, hsu->iolen, "HSU global")))
  1161. pr_warning("HSU: error in request mem region\n");
  1162. hsu->reg = ioremap_nocache((unsigned long)hsu->paddr, hsu->iolen);
  1163. if (!hsu->reg) {
  1164. pr_err("HSU: error in ioremap\n");
  1165. ret = -ENOMEM;
  1166. goto err_free_region;
  1167. }
  1168. /* Initialise the 3 UART ports */
  1169. uport = hsu->port;
  1170. for (i = 0; i < 3; i++) {
  1171. uport->port.type = PORT_MFD;
  1172. uport->port.iotype = UPIO_MEM;
  1173. uport->port.mapbase = (resource_size_t)hsu->paddr
  1174. + HSU_PORT_REG_OFFSET
  1175. + i * HSU_PORT_REG_LENGTH;
  1176. uport->port.membase = hsu->reg + HSU_PORT_REG_OFFSET
  1177. + i * HSU_PORT_REG_LENGTH;
  1178. sprintf(uport->name, "hsu_port%d", i);
  1179. uport->port.fifosize = 64;
  1180. uport->port.ops = &serial_hsu_pops;
  1181. uport->port.line = i;
  1182. uport->port.flags = UPF_IOREMAP;
  1183. /* set the scalable maxim support rate to 2746800 bps */
  1184. uport->port.uartclk = 115200 * 24 * 16;
  1185. uport->running = 0;
  1186. uport->txc = &hsu->chans[i * 2];
  1187. uport->rxc = &hsu->chans[i * 2 + 1];
  1188. serial_hsu_ports[i] = uport;
  1189. uport->index = i;
  1190. uport++;
  1191. }
  1192. /* Initialise 6 dma channels */
  1193. dchan = hsu->chans;
  1194. for (i = 0; i < 6; i++) {
  1195. dchan->id = i;
  1196. dchan->dirt = (i & 0x1) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1197. dchan->uport = &hsu->port[i/2];
  1198. dchan->reg = hsu->reg + HSU_DMA_CHANS_REG_OFFSET +
  1199. i * HSU_DMA_CHANS_REG_LENGTH;
  1200. /* Work around for RX */
  1201. if (dchan->dirt == DMA_FROM_DEVICE) {
  1202. init_timer(&dchan->rx_timer);
  1203. dchan->rx_timer.function = hsu_dma_rx_timeout;
  1204. dchan->rx_timer.data = (unsigned long)dchan;
  1205. }
  1206. dchan++;
  1207. }
  1208. phsu = hsu;
  1209. hsu_debugfs_init(hsu);
  1210. return;
  1211. err_free_region:
  1212. release_mem_region(hsu->paddr, hsu->iolen);
  1213. kfree(hsu);
  1214. return;
  1215. }
  1216. static void serial_hsu_remove(struct pci_dev *pdev)
  1217. {
  1218. void *priv = pci_get_drvdata(pdev);
  1219. struct uart_hsu_port *up;
  1220. if (!priv)
  1221. return;
  1222. /* For port 0/1/2, priv is the address of uart_hsu_port */
  1223. if (pdev->device != 0x081E) {
  1224. up = priv;
  1225. uart_remove_one_port(&serial_hsu_reg, &up->port);
  1226. }
  1227. pci_set_drvdata(pdev, NULL);
  1228. free_irq(pdev->irq, priv);
  1229. pci_disable_device(pdev);
  1230. }
  1231. /* First 3 are UART ports, and the 4th is the DMA */
  1232. static const struct pci_device_id pci_ids[] __devinitdata = {
  1233. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081B) },
  1234. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081C) },
  1235. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081D) },
  1236. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081E) },
  1237. {},
  1238. };
  1239. static struct pci_driver hsu_pci_driver = {
  1240. .name = "HSU serial",
  1241. .id_table = pci_ids,
  1242. .probe = serial_hsu_probe,
  1243. .remove = __devexit_p(serial_hsu_remove),
  1244. .suspend = serial_hsu_suspend,
  1245. .resume = serial_hsu_resume,
  1246. };
  1247. static int __init hsu_pci_init(void)
  1248. {
  1249. int ret;
  1250. hsu_global_init();
  1251. ret = uart_register_driver(&serial_hsu_reg);
  1252. if (ret)
  1253. return ret;
  1254. return pci_register_driver(&hsu_pci_driver);
  1255. }
  1256. static void __exit hsu_pci_exit(void)
  1257. {
  1258. pci_unregister_driver(&hsu_pci_driver);
  1259. uart_unregister_driver(&serial_hsu_reg);
  1260. hsu_debugfs_remove(phsu);
  1261. kfree(phsu);
  1262. }
  1263. module_init(hsu_pci_init);
  1264. module_exit(hsu_pci_exit);
  1265. MODULE_LICENSE("GPL v2");
  1266. MODULE_ALIAS("platform:medfield-hsu");