mfd.c 36 KB

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