mfd.c 36 KB

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