pch_uart.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. /*
  2. *Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
  3. *
  4. *This program is free software; you can redistribute it and/or modify
  5. *it under the terms of the GNU General Public License as published by
  6. *the Free Software Foundation; version 2 of the License.
  7. *
  8. *This program is distributed in the hope that it will be useful,
  9. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. *GNU General Public License for more details.
  12. *
  13. *You should have received a copy of the GNU General Public License
  14. *along with this program; if not, write to the Free Software
  15. *Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/serial_reg.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/dmi.h>
  28. #include <linux/console.h>
  29. #include <linux/nmi.h>
  30. #include <linux/delay.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/dmaengine.h>
  33. #include <linux/pch_dma.h>
  34. enum {
  35. PCH_UART_HANDLED_RX_INT_SHIFT,
  36. PCH_UART_HANDLED_TX_INT_SHIFT,
  37. PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
  38. PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
  39. PCH_UART_HANDLED_MS_INT_SHIFT,
  40. };
  41. enum {
  42. PCH_UART_8LINE,
  43. PCH_UART_2LINE,
  44. };
  45. #define PCH_UART_DRIVER_DEVICE "ttyPCH"
  46. /* Set the max number of UART port
  47. * Intel EG20T PCH: 4 port
  48. * LAPIS Semiconductor ML7213 IOH: 3 port
  49. * LAPIS Semiconductor ML7223 IOH: 2 port
  50. */
  51. #define PCH_UART_NR 4
  52. #define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
  53. #define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
  54. #define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
  55. PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
  56. #define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
  57. PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
  58. #define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
  59. #define PCH_UART_RBR 0x00
  60. #define PCH_UART_THR 0x00
  61. #define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
  62. PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
  63. #define PCH_UART_IER_ERBFI 0x00000001
  64. #define PCH_UART_IER_ETBEI 0x00000002
  65. #define PCH_UART_IER_ELSI 0x00000004
  66. #define PCH_UART_IER_EDSSI 0x00000008
  67. #define PCH_UART_IIR_IP 0x00000001
  68. #define PCH_UART_IIR_IID 0x00000006
  69. #define PCH_UART_IIR_MSI 0x00000000
  70. #define PCH_UART_IIR_TRI 0x00000002
  71. #define PCH_UART_IIR_RRI 0x00000004
  72. #define PCH_UART_IIR_REI 0x00000006
  73. #define PCH_UART_IIR_TOI 0x00000008
  74. #define PCH_UART_IIR_FIFO256 0x00000020
  75. #define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
  76. #define PCH_UART_IIR_FE 0x000000C0
  77. #define PCH_UART_FCR_FIFOE 0x00000001
  78. #define PCH_UART_FCR_RFR 0x00000002
  79. #define PCH_UART_FCR_TFR 0x00000004
  80. #define PCH_UART_FCR_DMS 0x00000008
  81. #define PCH_UART_FCR_FIFO256 0x00000020
  82. #define PCH_UART_FCR_RFTL 0x000000C0
  83. #define PCH_UART_FCR_RFTL1 0x00000000
  84. #define PCH_UART_FCR_RFTL64 0x00000040
  85. #define PCH_UART_FCR_RFTL128 0x00000080
  86. #define PCH_UART_FCR_RFTL224 0x000000C0
  87. #define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
  88. #define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
  89. #define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
  90. #define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
  91. #define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
  92. #define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
  93. #define PCH_UART_FCR_RFTL_SHIFT 6
  94. #define PCH_UART_LCR_WLS 0x00000003
  95. #define PCH_UART_LCR_STB 0x00000004
  96. #define PCH_UART_LCR_PEN 0x00000008
  97. #define PCH_UART_LCR_EPS 0x00000010
  98. #define PCH_UART_LCR_SP 0x00000020
  99. #define PCH_UART_LCR_SB 0x00000040
  100. #define PCH_UART_LCR_DLAB 0x00000080
  101. #define PCH_UART_LCR_NP 0x00000000
  102. #define PCH_UART_LCR_OP PCH_UART_LCR_PEN
  103. #define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
  104. #define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
  105. #define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
  106. PCH_UART_LCR_SP)
  107. #define PCH_UART_LCR_5BIT 0x00000000
  108. #define PCH_UART_LCR_6BIT 0x00000001
  109. #define PCH_UART_LCR_7BIT 0x00000002
  110. #define PCH_UART_LCR_8BIT 0x00000003
  111. #define PCH_UART_MCR_DTR 0x00000001
  112. #define PCH_UART_MCR_RTS 0x00000002
  113. #define PCH_UART_MCR_OUT 0x0000000C
  114. #define PCH_UART_MCR_LOOP 0x00000010
  115. #define PCH_UART_MCR_AFE 0x00000020
  116. #define PCH_UART_LSR_DR 0x00000001
  117. #define PCH_UART_LSR_ERR (1<<7)
  118. #define PCH_UART_MSR_DCTS 0x00000001
  119. #define PCH_UART_MSR_DDSR 0x00000002
  120. #define PCH_UART_MSR_TERI 0x00000004
  121. #define PCH_UART_MSR_DDCD 0x00000008
  122. #define PCH_UART_MSR_CTS 0x00000010
  123. #define PCH_UART_MSR_DSR 0x00000020
  124. #define PCH_UART_MSR_RI 0x00000040
  125. #define PCH_UART_MSR_DCD 0x00000080
  126. #define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
  127. PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
  128. #define PCH_UART_DLL 0x00
  129. #define PCH_UART_DLM 0x01
  130. #define PCH_UART_BRCSR 0x0E
  131. #define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
  132. #define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
  133. #define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
  134. #define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
  135. #define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
  136. #define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
  137. #define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
  138. #define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
  139. #define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
  140. #define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
  141. #define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
  142. #define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
  143. #define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
  144. #define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
  145. #define PCH_UART_HAL_STB1 0
  146. #define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
  147. #define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
  148. #define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
  149. #define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
  150. PCH_UART_HAL_CLR_RX_FIFO)
  151. #define PCH_UART_HAL_DMA_MODE0 0
  152. #define PCH_UART_HAL_FIFO_DIS 0
  153. #define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
  154. #define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
  155. PCH_UART_FCR_FIFO256)
  156. #define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
  157. #define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
  158. #define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
  159. #define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
  160. #define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
  161. #define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
  162. #define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
  163. #define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
  164. #define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
  165. #define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
  166. #define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
  167. #define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
  168. #define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
  169. #define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
  170. #define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
  171. #define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
  172. #define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
  173. #define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
  174. #define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
  175. #define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
  176. #define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
  177. #define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
  178. #define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
  179. #define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
  180. #define PCI_VENDOR_ID_ROHM 0x10DB
  181. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  182. #define DEFAULT_UARTCLK 1843200 /* 1.8432 MHz */
  183. #define CMITC_UARTCLK 192000000 /* 192.0000 MHz */
  184. #define FRI2_64_UARTCLK 64000000 /* 64.0000 MHz */
  185. #define FRI2_48_UARTCLK 48000000 /* 48.0000 MHz */
  186. #define NTC1_UARTCLK 64000000 /* 64.0000 MHz */
  187. struct pch_uart_buffer {
  188. unsigned char *buf;
  189. int size;
  190. };
  191. struct eg20t_port {
  192. struct uart_port port;
  193. int port_type;
  194. void __iomem *membase;
  195. resource_size_t mapbase;
  196. unsigned int iobase;
  197. struct pci_dev *pdev;
  198. int fifo_size;
  199. int uartclk;
  200. int start_tx;
  201. int start_rx;
  202. int tx_empty;
  203. int int_dis_flag;
  204. int trigger;
  205. int trigger_level;
  206. struct pch_uart_buffer rxbuf;
  207. unsigned int dmsr;
  208. unsigned int fcr;
  209. unsigned int mcr;
  210. unsigned int use_dma;
  211. unsigned int use_dma_flag;
  212. struct dma_async_tx_descriptor *desc_tx;
  213. struct dma_async_tx_descriptor *desc_rx;
  214. struct pch_dma_slave param_tx;
  215. struct pch_dma_slave param_rx;
  216. struct dma_chan *chan_tx;
  217. struct dma_chan *chan_rx;
  218. struct scatterlist *sg_tx_p;
  219. int nent;
  220. struct scatterlist sg_rx;
  221. int tx_dma_use;
  222. void *rx_buf_virt;
  223. dma_addr_t rx_buf_dma;
  224. struct dentry *debugfs;
  225. };
  226. /**
  227. * struct pch_uart_driver_data - private data structure for UART-DMA
  228. * @port_type: The number of DMA channel
  229. * @line_no: UART port line number (0, 1, 2...)
  230. */
  231. struct pch_uart_driver_data {
  232. int port_type;
  233. int line_no;
  234. };
  235. enum pch_uart_num_t {
  236. pch_et20t_uart0 = 0,
  237. pch_et20t_uart1,
  238. pch_et20t_uart2,
  239. pch_et20t_uart3,
  240. pch_ml7213_uart0,
  241. pch_ml7213_uart1,
  242. pch_ml7213_uart2,
  243. pch_ml7223_uart0,
  244. pch_ml7223_uart1,
  245. pch_ml7831_uart0,
  246. pch_ml7831_uart1,
  247. };
  248. static struct pch_uart_driver_data drv_dat[] = {
  249. [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
  250. [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
  251. [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
  252. [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
  253. [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
  254. [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
  255. [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
  256. [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
  257. [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
  258. [pch_ml7831_uart0] = {PCH_UART_8LINE, 0},
  259. [pch_ml7831_uart1] = {PCH_UART_2LINE, 1},
  260. };
  261. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  262. static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
  263. #endif
  264. static unsigned int default_baud = 9600;
  265. static unsigned int user_uartclk = 0;
  266. static const int trigger_level_256[4] = { 1, 64, 128, 224 };
  267. static const int trigger_level_64[4] = { 1, 16, 32, 56 };
  268. static const int trigger_level_16[4] = { 1, 4, 8, 14 };
  269. static const int trigger_level_1[4] = { 1, 1, 1, 1 };
  270. #ifdef CONFIG_DEBUG_FS
  271. #define PCH_REGS_BUFSIZE 1024
  272. static int pch_show_regs_open(struct inode *inode, struct file *file)
  273. {
  274. file->private_data = inode->i_private;
  275. return 0;
  276. }
  277. static ssize_t port_show_regs(struct file *file, char __user *user_buf,
  278. size_t count, loff_t *ppos)
  279. {
  280. struct eg20t_port *priv = file->private_data;
  281. char *buf;
  282. u32 len = 0;
  283. ssize_t ret;
  284. unsigned char lcr;
  285. buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
  286. if (!buf)
  287. return 0;
  288. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  289. "PCH EG20T port[%d] regs:\n", priv->port.line);
  290. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  291. "=================================\n");
  292. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  293. "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
  294. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  295. "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
  296. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  297. "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
  298. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  299. "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
  300. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  301. "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
  302. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  303. "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
  304. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  305. "BRCSR: \t0x%02x\n",
  306. ioread8(priv->membase + PCH_UART_BRCSR));
  307. lcr = ioread8(priv->membase + UART_LCR);
  308. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  309. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  310. "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
  311. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  312. "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
  313. iowrite8(lcr, priv->membase + UART_LCR);
  314. if (len > PCH_REGS_BUFSIZE)
  315. len = PCH_REGS_BUFSIZE;
  316. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  317. kfree(buf);
  318. return ret;
  319. }
  320. static const struct file_operations port_regs_ops = {
  321. .owner = THIS_MODULE,
  322. .open = pch_show_regs_open,
  323. .read = port_show_regs,
  324. .llseek = default_llseek,
  325. };
  326. #endif /* CONFIG_DEBUG_FS */
  327. /* Return UART clock, checking for board specific clocks. */
  328. static int pch_uart_get_uartclk(void)
  329. {
  330. const char *cmp;
  331. if (user_uartclk)
  332. return user_uartclk;
  333. cmp = dmi_get_system_info(DMI_BOARD_NAME);
  334. if (cmp && strstr(cmp, "CM-iTC"))
  335. return CMITC_UARTCLK;
  336. cmp = dmi_get_system_info(DMI_BIOS_VERSION);
  337. if (cmp && strnstr(cmp, "FRI2", 4))
  338. return FRI2_64_UARTCLK;
  339. cmp = dmi_get_system_info(DMI_PRODUCT_NAME);
  340. if (cmp && strstr(cmp, "Fish River Island II"))
  341. return FRI2_48_UARTCLK;
  342. /* Kontron COMe-mTT10 (nanoETXexpress-TT) */
  343. cmp = dmi_get_system_info(DMI_BOARD_NAME);
  344. if (cmp && (strstr(cmp, "COMe-mTT") ||
  345. strstr(cmp, "nanoETXexpress-TT")))
  346. return NTC1_UARTCLK;
  347. return DEFAULT_UARTCLK;
  348. }
  349. static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
  350. unsigned int flag)
  351. {
  352. u8 ier = ioread8(priv->membase + UART_IER);
  353. ier |= flag & PCH_UART_IER_MASK;
  354. iowrite8(ier, priv->membase + UART_IER);
  355. }
  356. static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
  357. unsigned int flag)
  358. {
  359. u8 ier = ioread8(priv->membase + UART_IER);
  360. ier &= ~(flag & PCH_UART_IER_MASK);
  361. iowrite8(ier, priv->membase + UART_IER);
  362. }
  363. static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
  364. unsigned int parity, unsigned int bits,
  365. unsigned int stb)
  366. {
  367. unsigned int dll, dlm, lcr;
  368. int div;
  369. div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
  370. if (div < 0 || USHRT_MAX <= div) {
  371. dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
  372. return -EINVAL;
  373. }
  374. dll = (unsigned int)div & 0x00FFU;
  375. dlm = ((unsigned int)div >> 8) & 0x00FFU;
  376. if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
  377. dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
  378. return -EINVAL;
  379. }
  380. if (bits & ~PCH_UART_LCR_WLS) {
  381. dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
  382. return -EINVAL;
  383. }
  384. if (stb & ~PCH_UART_LCR_STB) {
  385. dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
  386. return -EINVAL;
  387. }
  388. lcr = parity;
  389. lcr |= bits;
  390. lcr |= stb;
  391. dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
  392. __func__, baud, div, lcr, jiffies);
  393. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  394. iowrite8(dll, priv->membase + PCH_UART_DLL);
  395. iowrite8(dlm, priv->membase + PCH_UART_DLM);
  396. iowrite8(lcr, priv->membase + UART_LCR);
  397. return 0;
  398. }
  399. static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
  400. unsigned int flag)
  401. {
  402. if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
  403. dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
  404. __func__, flag);
  405. return -EINVAL;
  406. }
  407. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
  408. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
  409. priv->membase + UART_FCR);
  410. iowrite8(priv->fcr, priv->membase + UART_FCR);
  411. return 0;
  412. }
  413. static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
  414. unsigned int dmamode,
  415. unsigned int fifo_size, unsigned int trigger)
  416. {
  417. u8 fcr;
  418. if (dmamode & ~PCH_UART_FCR_DMS) {
  419. dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
  420. __func__, dmamode);
  421. return -EINVAL;
  422. }
  423. if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
  424. dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
  425. __func__, fifo_size);
  426. return -EINVAL;
  427. }
  428. if (trigger & ~PCH_UART_FCR_RFTL) {
  429. dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
  430. __func__, trigger);
  431. return -EINVAL;
  432. }
  433. switch (priv->fifo_size) {
  434. case 256:
  435. priv->trigger_level =
  436. trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  437. break;
  438. case 64:
  439. priv->trigger_level =
  440. trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  441. break;
  442. case 16:
  443. priv->trigger_level =
  444. trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  445. break;
  446. default:
  447. priv->trigger_level =
  448. trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  449. break;
  450. }
  451. fcr =
  452. dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
  453. iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
  454. iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
  455. priv->membase + UART_FCR);
  456. iowrite8(fcr, priv->membase + UART_FCR);
  457. priv->fcr = fcr;
  458. return 0;
  459. }
  460. static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
  461. {
  462. unsigned int msr = ioread8(priv->membase + UART_MSR);
  463. priv->dmsr = msr & PCH_UART_MSR_DELTA;
  464. return (u8)msr;
  465. }
  466. static void pch_uart_hal_write(struct eg20t_port *priv,
  467. const unsigned char *buf, int tx_size)
  468. {
  469. int i;
  470. unsigned int thr;
  471. for (i = 0; i < tx_size;) {
  472. thr = buf[i++];
  473. iowrite8(thr, priv->membase + PCH_UART_THR);
  474. }
  475. }
  476. static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
  477. int rx_size)
  478. {
  479. int i;
  480. u8 rbr, lsr;
  481. lsr = ioread8(priv->membase + UART_LSR);
  482. for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
  483. i < rx_size && lsr & UART_LSR_DR;
  484. lsr = ioread8(priv->membase + UART_LSR)) {
  485. rbr = ioread8(priv->membase + PCH_UART_RBR);
  486. buf[i++] = rbr;
  487. }
  488. return i;
  489. }
  490. static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
  491. {
  492. unsigned int iir;
  493. int ret;
  494. iir = ioread8(priv->membase + UART_IIR);
  495. ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
  496. return ret;
  497. }
  498. static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
  499. {
  500. return ioread8(priv->membase + UART_LSR);
  501. }
  502. static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
  503. {
  504. unsigned int lcr;
  505. lcr = ioread8(priv->membase + UART_LCR);
  506. if (on)
  507. lcr |= PCH_UART_LCR_SB;
  508. else
  509. lcr &= ~PCH_UART_LCR_SB;
  510. iowrite8(lcr, priv->membase + UART_LCR);
  511. }
  512. static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
  513. int size)
  514. {
  515. struct uart_port *port;
  516. struct tty_struct *tty;
  517. port = &priv->port;
  518. tty = tty_port_tty_get(&port->state->port);
  519. if (!tty) {
  520. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  521. return -EBUSY;
  522. }
  523. tty_insert_flip_string(tty, buf, size);
  524. tty_flip_buffer_push(tty);
  525. tty_kref_put(tty);
  526. return 0;
  527. }
  528. static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
  529. {
  530. int ret = 0;
  531. struct uart_port *port = &priv->port;
  532. if (port->x_char) {
  533. dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
  534. __func__, port->x_char, jiffies);
  535. buf[0] = port->x_char;
  536. port->x_char = 0;
  537. ret = 1;
  538. }
  539. return ret;
  540. }
  541. static int dma_push_rx(struct eg20t_port *priv, int size)
  542. {
  543. struct tty_struct *tty;
  544. int room;
  545. struct uart_port *port = &priv->port;
  546. port = &priv->port;
  547. tty = tty_port_tty_get(&port->state->port);
  548. if (!tty) {
  549. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  550. return 0;
  551. }
  552. room = tty_buffer_request_room(tty, size);
  553. if (room < size)
  554. dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
  555. size - room);
  556. if (!room)
  557. return room;
  558. tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
  559. port->icount.rx += room;
  560. tty_kref_put(tty);
  561. return room;
  562. }
  563. static void pch_free_dma(struct uart_port *port)
  564. {
  565. struct eg20t_port *priv;
  566. priv = container_of(port, struct eg20t_port, port);
  567. if (priv->chan_tx) {
  568. dma_release_channel(priv->chan_tx);
  569. priv->chan_tx = NULL;
  570. }
  571. if (priv->chan_rx) {
  572. dma_release_channel(priv->chan_rx);
  573. priv->chan_rx = NULL;
  574. }
  575. if (sg_dma_address(&priv->sg_rx))
  576. dma_free_coherent(port->dev, port->fifosize,
  577. sg_virt(&priv->sg_rx),
  578. sg_dma_address(&priv->sg_rx));
  579. return;
  580. }
  581. static bool filter(struct dma_chan *chan, void *slave)
  582. {
  583. struct pch_dma_slave *param = slave;
  584. if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
  585. chan->device->dev)) {
  586. chan->private = param;
  587. return true;
  588. } else {
  589. return false;
  590. }
  591. }
  592. static void pch_request_dma(struct uart_port *port)
  593. {
  594. dma_cap_mask_t mask;
  595. struct dma_chan *chan;
  596. struct pci_dev *dma_dev;
  597. struct pch_dma_slave *param;
  598. struct eg20t_port *priv =
  599. container_of(port, struct eg20t_port, port);
  600. dma_cap_zero(mask);
  601. dma_cap_set(DMA_SLAVE, mask);
  602. dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
  603. PCI_DEVFN(0xa, 0)); /* Get DMA's dev
  604. information */
  605. /* Set Tx DMA */
  606. param = &priv->param_tx;
  607. param->dma_dev = &dma_dev->dev;
  608. param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
  609. param->tx_reg = port->mapbase + UART_TX;
  610. chan = dma_request_channel(mask, filter, param);
  611. if (!chan) {
  612. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
  613. __func__);
  614. return;
  615. }
  616. priv->chan_tx = chan;
  617. /* Set Rx DMA */
  618. param = &priv->param_rx;
  619. param->dma_dev = &dma_dev->dev;
  620. param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
  621. param->rx_reg = port->mapbase + UART_RX;
  622. chan = dma_request_channel(mask, filter, param);
  623. if (!chan) {
  624. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
  625. __func__);
  626. dma_release_channel(priv->chan_tx);
  627. priv->chan_tx = NULL;
  628. return;
  629. }
  630. /* Get Consistent memory for DMA */
  631. priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
  632. &priv->rx_buf_dma, GFP_KERNEL);
  633. priv->chan_rx = chan;
  634. }
  635. static void pch_dma_rx_complete(void *arg)
  636. {
  637. struct eg20t_port *priv = arg;
  638. struct uart_port *port = &priv->port;
  639. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  640. int count;
  641. if (!tty) {
  642. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  643. return;
  644. }
  645. dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
  646. count = dma_push_rx(priv, priv->trigger_level);
  647. if (count)
  648. tty_flip_buffer_push(tty);
  649. tty_kref_put(tty);
  650. async_tx_ack(priv->desc_rx);
  651. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
  652. }
  653. static void pch_dma_tx_complete(void *arg)
  654. {
  655. struct eg20t_port *priv = arg;
  656. struct uart_port *port = &priv->port;
  657. struct circ_buf *xmit = &port->state->xmit;
  658. struct scatterlist *sg = priv->sg_tx_p;
  659. int i;
  660. for (i = 0; i < priv->nent; i++, sg++) {
  661. xmit->tail += sg_dma_len(sg);
  662. port->icount.tx += sg_dma_len(sg);
  663. }
  664. xmit->tail &= UART_XMIT_SIZE - 1;
  665. async_tx_ack(priv->desc_tx);
  666. dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
  667. priv->tx_dma_use = 0;
  668. priv->nent = 0;
  669. kfree(priv->sg_tx_p);
  670. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  671. }
  672. static int pop_tx(struct eg20t_port *priv, int size)
  673. {
  674. int count = 0;
  675. struct uart_port *port = &priv->port;
  676. struct circ_buf *xmit = &port->state->xmit;
  677. if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
  678. goto pop_tx_end;
  679. do {
  680. int cnt_to_end =
  681. CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  682. int sz = min(size - count, cnt_to_end);
  683. pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
  684. xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
  685. count += sz;
  686. } while (!uart_circ_empty(xmit) && count < size);
  687. pop_tx_end:
  688. dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
  689. count, size - count, jiffies);
  690. return count;
  691. }
  692. static int handle_rx_to(struct eg20t_port *priv)
  693. {
  694. struct pch_uart_buffer *buf;
  695. int rx_size;
  696. int ret;
  697. if (!priv->start_rx) {
  698. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
  699. return 0;
  700. }
  701. buf = &priv->rxbuf;
  702. do {
  703. rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
  704. ret = push_rx(priv, buf->buf, rx_size);
  705. if (ret)
  706. return 0;
  707. } while (rx_size == buf->size);
  708. return PCH_UART_HANDLED_RX_INT;
  709. }
  710. static int handle_rx(struct eg20t_port *priv)
  711. {
  712. return handle_rx_to(priv);
  713. }
  714. static int dma_handle_rx(struct eg20t_port *priv)
  715. {
  716. struct uart_port *port = &priv->port;
  717. struct dma_async_tx_descriptor *desc;
  718. struct scatterlist *sg;
  719. priv = container_of(port, struct eg20t_port, port);
  720. sg = &priv->sg_rx;
  721. sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
  722. sg_dma_len(sg) = priv->trigger_level;
  723. sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
  724. sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
  725. ~PAGE_MASK);
  726. sg_dma_address(sg) = priv->rx_buf_dma;
  727. desc = dmaengine_prep_slave_sg(priv->chan_rx,
  728. sg, 1, DMA_DEV_TO_MEM,
  729. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  730. if (!desc)
  731. return 0;
  732. priv->desc_rx = desc;
  733. desc->callback = pch_dma_rx_complete;
  734. desc->callback_param = priv;
  735. desc->tx_submit(desc);
  736. dma_async_issue_pending(priv->chan_rx);
  737. return PCH_UART_HANDLED_RX_INT;
  738. }
  739. static unsigned int handle_tx(struct eg20t_port *priv)
  740. {
  741. struct uart_port *port = &priv->port;
  742. struct circ_buf *xmit = &port->state->xmit;
  743. int fifo_size;
  744. int tx_size;
  745. int size;
  746. int tx_empty;
  747. if (!priv->start_tx) {
  748. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  749. __func__, jiffies);
  750. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  751. priv->tx_empty = 1;
  752. return 0;
  753. }
  754. fifo_size = max(priv->fifo_size, 1);
  755. tx_empty = 1;
  756. if (pop_tx_x(priv, xmit->buf)) {
  757. pch_uart_hal_write(priv, xmit->buf, 1);
  758. port->icount.tx++;
  759. tx_empty = 0;
  760. fifo_size--;
  761. }
  762. size = min(xmit->head - xmit->tail, fifo_size);
  763. if (size < 0)
  764. size = fifo_size;
  765. tx_size = pop_tx(priv, size);
  766. if (tx_size > 0) {
  767. port->icount.tx += tx_size;
  768. tx_empty = 0;
  769. }
  770. priv->tx_empty = tx_empty;
  771. if (tx_empty) {
  772. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  773. uart_write_wakeup(port);
  774. }
  775. return PCH_UART_HANDLED_TX_INT;
  776. }
  777. static unsigned int dma_handle_tx(struct eg20t_port *priv)
  778. {
  779. struct uart_port *port = &priv->port;
  780. struct circ_buf *xmit = &port->state->xmit;
  781. struct scatterlist *sg;
  782. int nent;
  783. int fifo_size;
  784. int tx_empty;
  785. struct dma_async_tx_descriptor *desc;
  786. int num;
  787. int i;
  788. int bytes;
  789. int size;
  790. int rem;
  791. if (!priv->start_tx) {
  792. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  793. __func__, jiffies);
  794. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  795. priv->tx_empty = 1;
  796. return 0;
  797. }
  798. if (priv->tx_dma_use) {
  799. dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
  800. __func__, jiffies);
  801. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  802. priv->tx_empty = 1;
  803. return 0;
  804. }
  805. fifo_size = max(priv->fifo_size, 1);
  806. tx_empty = 1;
  807. if (pop_tx_x(priv, xmit->buf)) {
  808. pch_uart_hal_write(priv, xmit->buf, 1);
  809. port->icount.tx++;
  810. tx_empty = 0;
  811. fifo_size--;
  812. }
  813. bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
  814. UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
  815. xmit->tail, UART_XMIT_SIZE));
  816. if (!bytes) {
  817. dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
  818. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  819. uart_write_wakeup(port);
  820. return 0;
  821. }
  822. if (bytes > fifo_size) {
  823. num = bytes / fifo_size + 1;
  824. size = fifo_size;
  825. rem = bytes % fifo_size;
  826. } else {
  827. num = 1;
  828. size = bytes;
  829. rem = bytes;
  830. }
  831. dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
  832. __func__, num, size, rem);
  833. priv->tx_dma_use = 1;
  834. priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
  835. sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
  836. sg = priv->sg_tx_p;
  837. for (i = 0; i < num; i++, sg++) {
  838. if (i == (num - 1))
  839. sg_set_page(sg, virt_to_page(xmit->buf),
  840. rem, fifo_size * i);
  841. else
  842. sg_set_page(sg, virt_to_page(xmit->buf),
  843. size, fifo_size * i);
  844. }
  845. sg = priv->sg_tx_p;
  846. nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
  847. if (!nent) {
  848. dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
  849. return 0;
  850. }
  851. priv->nent = nent;
  852. for (i = 0; i < nent; i++, sg++) {
  853. sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
  854. fifo_size * i;
  855. sg_dma_address(sg) = (sg_dma_address(sg) &
  856. ~(UART_XMIT_SIZE - 1)) + sg->offset;
  857. if (i == (nent - 1))
  858. sg_dma_len(sg) = rem;
  859. else
  860. sg_dma_len(sg) = size;
  861. }
  862. desc = dmaengine_prep_slave_sg(priv->chan_tx,
  863. priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
  864. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  865. if (!desc) {
  866. dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
  867. __func__);
  868. return 0;
  869. }
  870. dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
  871. priv->desc_tx = desc;
  872. desc->callback = pch_dma_tx_complete;
  873. desc->callback_param = priv;
  874. desc->tx_submit(desc);
  875. dma_async_issue_pending(priv->chan_tx);
  876. return PCH_UART_HANDLED_TX_INT;
  877. }
  878. static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
  879. {
  880. u8 fcr = ioread8(priv->membase + UART_FCR);
  881. /* Reset FIFO */
  882. fcr |= UART_FCR_CLEAR_RCVR;
  883. iowrite8(fcr, priv->membase + UART_FCR);
  884. if (lsr & PCH_UART_LSR_ERR)
  885. dev_err(&priv->pdev->dev, "Error data in FIFO\n");
  886. if (lsr & UART_LSR_FE)
  887. dev_err(&priv->pdev->dev, "Framing Error\n");
  888. if (lsr & UART_LSR_PE)
  889. dev_err(&priv->pdev->dev, "Parity Error\n");
  890. if (lsr & UART_LSR_OE)
  891. dev_err(&priv->pdev->dev, "Overrun Error\n");
  892. }
  893. static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
  894. {
  895. struct eg20t_port *priv = dev_id;
  896. unsigned int handled;
  897. u8 lsr;
  898. int ret = 0;
  899. unsigned int iid;
  900. unsigned long flags;
  901. spin_lock_irqsave(&priv->port.lock, flags);
  902. handled = 0;
  903. while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
  904. switch (iid) {
  905. case PCH_UART_IID_RLS: /* Receiver Line Status */
  906. lsr = pch_uart_hal_get_line_status(priv);
  907. if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
  908. UART_LSR_PE | UART_LSR_OE)) {
  909. pch_uart_err_ir(priv, lsr);
  910. ret = PCH_UART_HANDLED_RX_ERR_INT;
  911. }
  912. break;
  913. case PCH_UART_IID_RDR: /* Received Data Ready */
  914. if (priv->use_dma) {
  915. pch_uart_hal_disable_interrupt(priv,
  916. PCH_UART_HAL_RX_INT);
  917. ret = dma_handle_rx(priv);
  918. if (!ret)
  919. pch_uart_hal_enable_interrupt(priv,
  920. PCH_UART_HAL_RX_INT);
  921. } else {
  922. ret = handle_rx(priv);
  923. }
  924. break;
  925. case PCH_UART_IID_RDR_TO: /* Received Data Ready
  926. (FIFO Timeout) */
  927. ret = handle_rx_to(priv);
  928. break;
  929. case PCH_UART_IID_THRE: /* Transmitter Holding Register
  930. Empty */
  931. if (priv->use_dma)
  932. ret = dma_handle_tx(priv);
  933. else
  934. ret = handle_tx(priv);
  935. break;
  936. case PCH_UART_IID_MS: /* Modem Status */
  937. ret = PCH_UART_HANDLED_MS_INT;
  938. break;
  939. default: /* Never junp to this label */
  940. dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
  941. iid, jiffies);
  942. ret = -1;
  943. break;
  944. }
  945. handled |= (unsigned int)ret;
  946. }
  947. if (handled == 0 && iid <= 1) {
  948. if (priv->int_dis_flag)
  949. priv->int_dis_flag = 0;
  950. }
  951. spin_unlock_irqrestore(&priv->port.lock, flags);
  952. return IRQ_RETVAL(handled);
  953. }
  954. /* This function tests whether the transmitter fifo and shifter for the port
  955. described by 'port' is empty. */
  956. static unsigned int pch_uart_tx_empty(struct uart_port *port)
  957. {
  958. struct eg20t_port *priv;
  959. priv = container_of(port, struct eg20t_port, port);
  960. if (priv->tx_empty)
  961. return TIOCSER_TEMT;
  962. else
  963. return 0;
  964. }
  965. /* Returns the current state of modem control inputs. */
  966. static unsigned int pch_uart_get_mctrl(struct uart_port *port)
  967. {
  968. struct eg20t_port *priv;
  969. u8 modem;
  970. unsigned int ret = 0;
  971. priv = container_of(port, struct eg20t_port, port);
  972. modem = pch_uart_hal_get_modem(priv);
  973. if (modem & UART_MSR_DCD)
  974. ret |= TIOCM_CAR;
  975. if (modem & UART_MSR_RI)
  976. ret |= TIOCM_RNG;
  977. if (modem & UART_MSR_DSR)
  978. ret |= TIOCM_DSR;
  979. if (modem & UART_MSR_CTS)
  980. ret |= TIOCM_CTS;
  981. return ret;
  982. }
  983. static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  984. {
  985. u32 mcr = 0;
  986. struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
  987. if (mctrl & TIOCM_DTR)
  988. mcr |= UART_MCR_DTR;
  989. if (mctrl & TIOCM_RTS)
  990. mcr |= UART_MCR_RTS;
  991. if (mctrl & TIOCM_LOOP)
  992. mcr |= UART_MCR_LOOP;
  993. if (priv->mcr & UART_MCR_AFE)
  994. mcr |= UART_MCR_AFE;
  995. if (mctrl)
  996. iowrite8(mcr, priv->membase + UART_MCR);
  997. }
  998. static void pch_uart_stop_tx(struct uart_port *port)
  999. {
  1000. struct eg20t_port *priv;
  1001. priv = container_of(port, struct eg20t_port, port);
  1002. priv->start_tx = 0;
  1003. priv->tx_dma_use = 0;
  1004. }
  1005. static void pch_uart_start_tx(struct uart_port *port)
  1006. {
  1007. struct eg20t_port *priv;
  1008. priv = container_of(port, struct eg20t_port, port);
  1009. if (priv->use_dma) {
  1010. if (priv->tx_dma_use) {
  1011. dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
  1012. __func__);
  1013. return;
  1014. }
  1015. }
  1016. priv->start_tx = 1;
  1017. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  1018. }
  1019. static void pch_uart_stop_rx(struct uart_port *port)
  1020. {
  1021. struct eg20t_port *priv;
  1022. priv = container_of(port, struct eg20t_port, port);
  1023. priv->start_rx = 0;
  1024. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
  1025. priv->int_dis_flag = 1;
  1026. }
  1027. /* Enable the modem status interrupts. */
  1028. static void pch_uart_enable_ms(struct uart_port *port)
  1029. {
  1030. struct eg20t_port *priv;
  1031. priv = container_of(port, struct eg20t_port, port);
  1032. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
  1033. }
  1034. /* Control the transmission of a break signal. */
  1035. static void pch_uart_break_ctl(struct uart_port *port, int ctl)
  1036. {
  1037. struct eg20t_port *priv;
  1038. unsigned long flags;
  1039. priv = container_of(port, struct eg20t_port, port);
  1040. spin_lock_irqsave(&port->lock, flags);
  1041. pch_uart_hal_set_break(priv, ctl);
  1042. spin_unlock_irqrestore(&port->lock, flags);
  1043. }
  1044. /* Grab any interrupt resources and initialise any low level driver state. */
  1045. static int pch_uart_startup(struct uart_port *port)
  1046. {
  1047. struct eg20t_port *priv;
  1048. int ret;
  1049. int fifo_size;
  1050. int trigger_level;
  1051. priv = container_of(port, struct eg20t_port, port);
  1052. priv->tx_empty = 1;
  1053. if (port->uartclk)
  1054. priv->uartclk = port->uartclk;
  1055. else
  1056. port->uartclk = priv->uartclk;
  1057. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1058. ret = pch_uart_hal_set_line(priv, default_baud,
  1059. PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
  1060. PCH_UART_HAL_STB1);
  1061. if (ret)
  1062. return ret;
  1063. switch (priv->fifo_size) {
  1064. case 256:
  1065. fifo_size = PCH_UART_HAL_FIFO256;
  1066. break;
  1067. case 64:
  1068. fifo_size = PCH_UART_HAL_FIFO64;
  1069. break;
  1070. case 16:
  1071. fifo_size = PCH_UART_HAL_FIFO16;
  1072. case 1:
  1073. default:
  1074. fifo_size = PCH_UART_HAL_FIFO_DIS;
  1075. break;
  1076. }
  1077. switch (priv->trigger) {
  1078. case PCH_UART_HAL_TRIGGER1:
  1079. trigger_level = 1;
  1080. break;
  1081. case PCH_UART_HAL_TRIGGER_L:
  1082. trigger_level = priv->fifo_size / 4;
  1083. break;
  1084. case PCH_UART_HAL_TRIGGER_M:
  1085. trigger_level = priv->fifo_size / 2;
  1086. break;
  1087. case PCH_UART_HAL_TRIGGER_H:
  1088. default:
  1089. trigger_level = priv->fifo_size - (priv->fifo_size / 8);
  1090. break;
  1091. }
  1092. priv->trigger_level = trigger_level;
  1093. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1094. fifo_size, priv->trigger);
  1095. if (ret < 0)
  1096. return ret;
  1097. ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
  1098. KBUILD_MODNAME, priv);
  1099. if (ret < 0)
  1100. return ret;
  1101. if (priv->use_dma)
  1102. pch_request_dma(port);
  1103. priv->start_rx = 1;
  1104. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
  1105. uart_update_timeout(port, CS8, default_baud);
  1106. return 0;
  1107. }
  1108. static void pch_uart_shutdown(struct uart_port *port)
  1109. {
  1110. struct eg20t_port *priv;
  1111. int ret;
  1112. priv = container_of(port, struct eg20t_port, port);
  1113. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1114. pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
  1115. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1116. PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
  1117. if (ret)
  1118. dev_err(priv->port.dev,
  1119. "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
  1120. pch_free_dma(port);
  1121. free_irq(priv->port.irq, priv);
  1122. }
  1123. /* Change the port parameters, including word length, parity, stop
  1124. *bits. Update read_status_mask and ignore_status_mask to indicate
  1125. *the types of events we are interested in receiving. */
  1126. static void pch_uart_set_termios(struct uart_port *port,
  1127. struct ktermios *termios, struct ktermios *old)
  1128. {
  1129. int baud;
  1130. int rtn;
  1131. unsigned int parity, bits, stb;
  1132. struct eg20t_port *priv;
  1133. unsigned long flags;
  1134. priv = container_of(port, struct eg20t_port, port);
  1135. switch (termios->c_cflag & CSIZE) {
  1136. case CS5:
  1137. bits = PCH_UART_HAL_5BIT;
  1138. break;
  1139. case CS6:
  1140. bits = PCH_UART_HAL_6BIT;
  1141. break;
  1142. case CS7:
  1143. bits = PCH_UART_HAL_7BIT;
  1144. break;
  1145. default: /* CS8 */
  1146. bits = PCH_UART_HAL_8BIT;
  1147. break;
  1148. }
  1149. if (termios->c_cflag & CSTOPB)
  1150. stb = PCH_UART_HAL_STB2;
  1151. else
  1152. stb = PCH_UART_HAL_STB1;
  1153. if (termios->c_cflag & PARENB) {
  1154. if (!(termios->c_cflag & PARODD))
  1155. parity = PCH_UART_HAL_PARITY_ODD;
  1156. else
  1157. parity = PCH_UART_HAL_PARITY_EVEN;
  1158. } else
  1159. parity = PCH_UART_HAL_PARITY_NONE;
  1160. /* Only UART0 has auto hardware flow function */
  1161. if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
  1162. priv->mcr |= UART_MCR_AFE;
  1163. else
  1164. priv->mcr &= ~UART_MCR_AFE;
  1165. termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  1166. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1167. spin_lock_irqsave(&port->lock, flags);
  1168. uart_update_timeout(port, termios->c_cflag, baud);
  1169. rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
  1170. if (rtn)
  1171. goto out;
  1172. pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
  1173. /* Don't rewrite B0 */
  1174. if (tty_termios_baud_rate(termios))
  1175. tty_termios_encode_baud_rate(termios, baud, baud);
  1176. out:
  1177. spin_unlock_irqrestore(&port->lock, flags);
  1178. }
  1179. static const char *pch_uart_type(struct uart_port *port)
  1180. {
  1181. return KBUILD_MODNAME;
  1182. }
  1183. static void pch_uart_release_port(struct uart_port *port)
  1184. {
  1185. struct eg20t_port *priv;
  1186. priv = container_of(port, struct eg20t_port, port);
  1187. pci_iounmap(priv->pdev, priv->membase);
  1188. pci_release_regions(priv->pdev);
  1189. }
  1190. static int pch_uart_request_port(struct uart_port *port)
  1191. {
  1192. struct eg20t_port *priv;
  1193. int ret;
  1194. void __iomem *membase;
  1195. priv = container_of(port, struct eg20t_port, port);
  1196. ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
  1197. if (ret < 0)
  1198. return -EBUSY;
  1199. membase = pci_iomap(priv->pdev, 1, 0);
  1200. if (!membase) {
  1201. pci_release_regions(priv->pdev);
  1202. return -EBUSY;
  1203. }
  1204. priv->membase = port->membase = membase;
  1205. return 0;
  1206. }
  1207. static void pch_uart_config_port(struct uart_port *port, int type)
  1208. {
  1209. struct eg20t_port *priv;
  1210. priv = container_of(port, struct eg20t_port, port);
  1211. if (type & UART_CONFIG_TYPE) {
  1212. port->type = priv->port_type;
  1213. pch_uart_request_port(port);
  1214. }
  1215. }
  1216. static int pch_uart_verify_port(struct uart_port *port,
  1217. struct serial_struct *serinfo)
  1218. {
  1219. struct eg20t_port *priv;
  1220. priv = container_of(port, struct eg20t_port, port);
  1221. if (serinfo->flags & UPF_LOW_LATENCY) {
  1222. dev_info(priv->port.dev,
  1223. "PCH UART : Use PIO Mode (without DMA)\n");
  1224. priv->use_dma = 0;
  1225. serinfo->flags &= ~UPF_LOW_LATENCY;
  1226. } else {
  1227. #ifndef CONFIG_PCH_DMA
  1228. dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
  1229. __func__);
  1230. return -EOPNOTSUPP;
  1231. #endif
  1232. priv->use_dma = 1;
  1233. priv->use_dma_flag = 1;
  1234. dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
  1235. }
  1236. return 0;
  1237. }
  1238. static struct uart_ops pch_uart_ops = {
  1239. .tx_empty = pch_uart_tx_empty,
  1240. .set_mctrl = pch_uart_set_mctrl,
  1241. .get_mctrl = pch_uart_get_mctrl,
  1242. .stop_tx = pch_uart_stop_tx,
  1243. .start_tx = pch_uart_start_tx,
  1244. .stop_rx = pch_uart_stop_rx,
  1245. .enable_ms = pch_uart_enable_ms,
  1246. .break_ctl = pch_uart_break_ctl,
  1247. .startup = pch_uart_startup,
  1248. .shutdown = pch_uart_shutdown,
  1249. .set_termios = pch_uart_set_termios,
  1250. /* .pm = pch_uart_pm, Not supported yet */
  1251. /* .set_wake = pch_uart_set_wake, Not supported yet */
  1252. .type = pch_uart_type,
  1253. .release_port = pch_uart_release_port,
  1254. .request_port = pch_uart_request_port,
  1255. .config_port = pch_uart_config_port,
  1256. .verify_port = pch_uart_verify_port
  1257. };
  1258. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1259. /*
  1260. * Wait for transmitter & holding register to empty
  1261. */
  1262. static void wait_for_xmitr(struct eg20t_port *up, int bits)
  1263. {
  1264. unsigned int status, tmout = 10000;
  1265. /* Wait up to 10ms for the character(s) to be sent. */
  1266. for (;;) {
  1267. status = ioread8(up->membase + UART_LSR);
  1268. if ((status & bits) == bits)
  1269. break;
  1270. if (--tmout == 0)
  1271. break;
  1272. udelay(1);
  1273. }
  1274. /* Wait up to 1s for flow control if necessary */
  1275. if (up->port.flags & UPF_CONS_FLOW) {
  1276. unsigned int tmout;
  1277. for (tmout = 1000000; tmout; tmout--) {
  1278. unsigned int msr = ioread8(up->membase + UART_MSR);
  1279. if (msr & UART_MSR_CTS)
  1280. break;
  1281. udelay(1);
  1282. touch_nmi_watchdog();
  1283. }
  1284. }
  1285. }
  1286. static void pch_console_putchar(struct uart_port *port, int ch)
  1287. {
  1288. struct eg20t_port *priv =
  1289. container_of(port, struct eg20t_port, port);
  1290. wait_for_xmitr(priv, UART_LSR_THRE);
  1291. iowrite8(ch, priv->membase + PCH_UART_THR);
  1292. }
  1293. /*
  1294. * Print a string to the serial port trying not to disturb
  1295. * any possible real use of the port...
  1296. *
  1297. * The console_lock must be held when we get here.
  1298. */
  1299. static void
  1300. pch_console_write(struct console *co, const char *s, unsigned int count)
  1301. {
  1302. struct eg20t_port *priv;
  1303. unsigned long flags;
  1304. u8 ier;
  1305. int locked = 1;
  1306. priv = pch_uart_ports[co->index];
  1307. touch_nmi_watchdog();
  1308. local_irq_save(flags);
  1309. if (priv->port.sysrq) {
  1310. /* serial8250_handle_port() already took the lock */
  1311. locked = 0;
  1312. } else if (oops_in_progress) {
  1313. locked = spin_trylock(&priv->port.lock);
  1314. } else
  1315. spin_lock(&priv->port.lock);
  1316. /*
  1317. * First save the IER then disable the interrupts
  1318. */
  1319. ier = ioread8(priv->membase + UART_IER);
  1320. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1321. uart_console_write(&priv->port, s, count, pch_console_putchar);
  1322. /*
  1323. * Finally, wait for transmitter to become empty
  1324. * and restore the IER
  1325. */
  1326. wait_for_xmitr(priv, BOTH_EMPTY);
  1327. iowrite8(ier, priv->membase + UART_IER);
  1328. if (locked)
  1329. spin_unlock(&priv->port.lock);
  1330. local_irq_restore(flags);
  1331. }
  1332. static int __init pch_console_setup(struct console *co, char *options)
  1333. {
  1334. struct uart_port *port;
  1335. int baud = default_baud;
  1336. int bits = 8;
  1337. int parity = 'n';
  1338. int flow = 'n';
  1339. /*
  1340. * Check whether an invalid uart number has been specified, and
  1341. * if so, search for the first available port that does have
  1342. * console support.
  1343. */
  1344. if (co->index >= PCH_UART_NR)
  1345. co->index = 0;
  1346. port = &pch_uart_ports[co->index]->port;
  1347. if (!port || (!port->iobase && !port->membase))
  1348. return -ENODEV;
  1349. port->uartclk = pch_uart_get_uartclk();
  1350. if (options)
  1351. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1352. return uart_set_options(port, co, baud, parity, bits, flow);
  1353. }
  1354. static struct uart_driver pch_uart_driver;
  1355. static struct console pch_console = {
  1356. .name = PCH_UART_DRIVER_DEVICE,
  1357. .write = pch_console_write,
  1358. .device = uart_console_device,
  1359. .setup = pch_console_setup,
  1360. .flags = CON_PRINTBUFFER | CON_ANYTIME,
  1361. .index = -1,
  1362. .data = &pch_uart_driver,
  1363. };
  1364. #define PCH_CONSOLE (&pch_console)
  1365. #else
  1366. #define PCH_CONSOLE NULL
  1367. #endif
  1368. static struct uart_driver pch_uart_driver = {
  1369. .owner = THIS_MODULE,
  1370. .driver_name = KBUILD_MODNAME,
  1371. .dev_name = PCH_UART_DRIVER_DEVICE,
  1372. .major = 0,
  1373. .minor = 0,
  1374. .nr = PCH_UART_NR,
  1375. .cons = PCH_CONSOLE,
  1376. };
  1377. static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
  1378. const struct pci_device_id *id)
  1379. {
  1380. struct eg20t_port *priv;
  1381. int ret;
  1382. unsigned int iobase;
  1383. unsigned int mapbase;
  1384. unsigned char *rxbuf;
  1385. int fifosize;
  1386. int port_type;
  1387. struct pch_uart_driver_data *board;
  1388. char name[32]; /* for debugfs file name */
  1389. board = &drv_dat[id->driver_data];
  1390. port_type = board->port_type;
  1391. priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
  1392. if (priv == NULL)
  1393. goto init_port_alloc_err;
  1394. rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  1395. if (!rxbuf)
  1396. goto init_port_free_txbuf;
  1397. switch (port_type) {
  1398. case PORT_UNKNOWN:
  1399. fifosize = 256; /* EG20T/ML7213: UART0 */
  1400. break;
  1401. case PORT_8250:
  1402. fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
  1403. break;
  1404. default:
  1405. dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
  1406. goto init_port_hal_free;
  1407. }
  1408. pci_enable_msi(pdev);
  1409. pci_set_master(pdev);
  1410. iobase = pci_resource_start(pdev, 0);
  1411. mapbase = pci_resource_start(pdev, 1);
  1412. priv->mapbase = mapbase;
  1413. priv->iobase = iobase;
  1414. priv->pdev = pdev;
  1415. priv->tx_empty = 1;
  1416. priv->rxbuf.buf = rxbuf;
  1417. priv->rxbuf.size = PAGE_SIZE;
  1418. priv->fifo_size = fifosize;
  1419. priv->uartclk = pch_uart_get_uartclk();
  1420. priv->port_type = PORT_MAX_8250 + port_type + 1;
  1421. priv->port.dev = &pdev->dev;
  1422. priv->port.iobase = iobase;
  1423. priv->port.membase = NULL;
  1424. priv->port.mapbase = mapbase;
  1425. priv->port.irq = pdev->irq;
  1426. priv->port.iotype = UPIO_PORT;
  1427. priv->port.ops = &pch_uart_ops;
  1428. priv->port.flags = UPF_BOOT_AUTOCONF;
  1429. priv->port.fifosize = fifosize;
  1430. priv->port.line = board->line_no;
  1431. priv->trigger = PCH_UART_HAL_TRIGGER_M;
  1432. spin_lock_init(&priv->port.lock);
  1433. pci_set_drvdata(pdev, priv);
  1434. priv->trigger_level = 1;
  1435. priv->fcr = 0;
  1436. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1437. pch_uart_ports[board->line_no] = priv;
  1438. #endif
  1439. ret = uart_add_one_port(&pch_uart_driver, &priv->port);
  1440. if (ret < 0)
  1441. goto init_port_hal_free;
  1442. #ifdef CONFIG_DEBUG_FS
  1443. snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
  1444. priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
  1445. NULL, priv, &port_regs_ops);
  1446. #endif
  1447. return priv;
  1448. init_port_hal_free:
  1449. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1450. pch_uart_ports[board->line_no] = NULL;
  1451. #endif
  1452. free_page((unsigned long)rxbuf);
  1453. init_port_free_txbuf:
  1454. kfree(priv);
  1455. init_port_alloc_err:
  1456. return NULL;
  1457. }
  1458. static void pch_uart_exit_port(struct eg20t_port *priv)
  1459. {
  1460. #ifdef CONFIG_DEBUG_FS
  1461. if (priv->debugfs)
  1462. debugfs_remove(priv->debugfs);
  1463. #endif
  1464. uart_remove_one_port(&pch_uart_driver, &priv->port);
  1465. pci_set_drvdata(priv->pdev, NULL);
  1466. free_page((unsigned long)priv->rxbuf.buf);
  1467. }
  1468. static void pch_uart_pci_remove(struct pci_dev *pdev)
  1469. {
  1470. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1471. pci_disable_msi(pdev);
  1472. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1473. pch_uart_ports[priv->port.line] = NULL;
  1474. #endif
  1475. pch_uart_exit_port(priv);
  1476. pci_disable_device(pdev);
  1477. kfree(priv);
  1478. return;
  1479. }
  1480. #ifdef CONFIG_PM
  1481. static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1482. {
  1483. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1484. uart_suspend_port(&pch_uart_driver, &priv->port);
  1485. pci_save_state(pdev);
  1486. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1487. return 0;
  1488. }
  1489. static int pch_uart_pci_resume(struct pci_dev *pdev)
  1490. {
  1491. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1492. int ret;
  1493. pci_set_power_state(pdev, PCI_D0);
  1494. pci_restore_state(pdev);
  1495. ret = pci_enable_device(pdev);
  1496. if (ret) {
  1497. dev_err(&pdev->dev,
  1498. "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
  1499. return ret;
  1500. }
  1501. uart_resume_port(&pch_uart_driver, &priv->port);
  1502. return 0;
  1503. }
  1504. #else
  1505. #define pch_uart_pci_suspend NULL
  1506. #define pch_uart_pci_resume NULL
  1507. #endif
  1508. static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
  1509. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
  1510. .driver_data = pch_et20t_uart0},
  1511. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
  1512. .driver_data = pch_et20t_uart1},
  1513. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
  1514. .driver_data = pch_et20t_uart2},
  1515. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
  1516. .driver_data = pch_et20t_uart3},
  1517. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
  1518. .driver_data = pch_ml7213_uart0},
  1519. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
  1520. .driver_data = pch_ml7213_uart1},
  1521. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
  1522. .driver_data = pch_ml7213_uart2},
  1523. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
  1524. .driver_data = pch_ml7223_uart0},
  1525. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
  1526. .driver_data = pch_ml7223_uart1},
  1527. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
  1528. .driver_data = pch_ml7831_uart0},
  1529. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
  1530. .driver_data = pch_ml7831_uart1},
  1531. {0,},
  1532. };
  1533. static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
  1534. const struct pci_device_id *id)
  1535. {
  1536. int ret;
  1537. struct eg20t_port *priv;
  1538. ret = pci_enable_device(pdev);
  1539. if (ret < 0)
  1540. goto probe_error;
  1541. priv = pch_uart_init_port(pdev, id);
  1542. if (!priv) {
  1543. ret = -EBUSY;
  1544. goto probe_disable_device;
  1545. }
  1546. pci_set_drvdata(pdev, priv);
  1547. return ret;
  1548. probe_disable_device:
  1549. pci_disable_msi(pdev);
  1550. pci_disable_device(pdev);
  1551. probe_error:
  1552. return ret;
  1553. }
  1554. static struct pci_driver pch_uart_pci_driver = {
  1555. .name = "pch_uart",
  1556. .id_table = pch_uart_pci_id,
  1557. .probe = pch_uart_pci_probe,
  1558. .remove = __devexit_p(pch_uart_pci_remove),
  1559. .suspend = pch_uart_pci_suspend,
  1560. .resume = pch_uart_pci_resume,
  1561. };
  1562. static int __init pch_uart_module_init(void)
  1563. {
  1564. int ret;
  1565. /* register as UART driver */
  1566. ret = uart_register_driver(&pch_uart_driver);
  1567. if (ret < 0)
  1568. return ret;
  1569. /* register as PCI driver */
  1570. ret = pci_register_driver(&pch_uart_pci_driver);
  1571. if (ret < 0)
  1572. uart_unregister_driver(&pch_uart_driver);
  1573. return ret;
  1574. }
  1575. module_init(pch_uart_module_init);
  1576. static void __exit pch_uart_module_exit(void)
  1577. {
  1578. pci_unregister_driver(&pch_uart_pci_driver);
  1579. uart_unregister_driver(&pch_uart_driver);
  1580. }
  1581. module_exit(pch_uart_module_exit);
  1582. MODULE_LICENSE("GPL v2");
  1583. MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
  1584. module_param(default_baud, uint, S_IRUGO);
  1585. MODULE_PARM_DESC(default_baud,
  1586. "Default BAUD for initial driver state and console (default 9600)");
  1587. module_param(user_uartclk, uint, S_IRUGO);
  1588. MODULE_PARM_DESC(user_uartclk,
  1589. "Override UART default or board specific UART clock");