mfd.c 37 KB

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