pch_uart.c 48 KB

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