mfd.c 37 KB

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