mfd.c 36 KB

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