mfd.c 36 KB

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