mfd.c 36 KB

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