cx25821-core.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
  6. * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. *
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/i2c.h>
  25. #include <linux/slab.h>
  26. #include "cx25821.h"
  27. #include "cx25821-sram.h"
  28. #include "cx25821-video.h"
  29. MODULE_DESCRIPTION("Driver for Athena cards");
  30. MODULE_AUTHOR("Shu Lin - Hiep Huynh");
  31. MODULE_LICENSE("GPL");
  32. static unsigned int debug;
  33. module_param(debug, int, 0644);
  34. MODULE_PARM_DESC(debug, "enable debug messages");
  35. static unsigned int card[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
  36. module_param_array(card, int, NULL, 0444);
  37. MODULE_PARM_DESC(card, "card type");
  38. const struct sram_channel cx25821_sram_channels[] = {
  39. [SRAM_CH00] = {
  40. .i = SRAM_CH00,
  41. .name = "VID A",
  42. .cmds_start = VID_A_DOWN_CMDS,
  43. .ctrl_start = VID_A_IQ,
  44. .cdt = VID_A_CDT,
  45. .fifo_start = VID_A_DOWN_CLUSTER_1,
  46. .fifo_size = (VID_CLUSTER_SIZE << 2),
  47. .ptr1_reg = DMA1_PTR1,
  48. .ptr2_reg = DMA1_PTR2,
  49. .cnt1_reg = DMA1_CNT1,
  50. .cnt2_reg = DMA1_CNT2,
  51. .int_msk = VID_A_INT_MSK,
  52. .int_stat = VID_A_INT_STAT,
  53. .int_mstat = VID_A_INT_MSTAT,
  54. .dma_ctl = VID_DST_A_DMA_CTL,
  55. .gpcnt_ctl = VID_DST_A_GPCNT_CTL,
  56. .gpcnt = VID_DST_A_GPCNT,
  57. .vip_ctl = VID_DST_A_VIP_CTL,
  58. .pix_frmt = VID_DST_A_PIX_FRMT,
  59. },
  60. [SRAM_CH01] = {
  61. .i = SRAM_CH01,
  62. .name = "VID B",
  63. .cmds_start = VID_B_DOWN_CMDS,
  64. .ctrl_start = VID_B_IQ,
  65. .cdt = VID_B_CDT,
  66. .fifo_start = VID_B_DOWN_CLUSTER_1,
  67. .fifo_size = (VID_CLUSTER_SIZE << 2),
  68. .ptr1_reg = DMA2_PTR1,
  69. .ptr2_reg = DMA2_PTR2,
  70. .cnt1_reg = DMA2_CNT1,
  71. .cnt2_reg = DMA2_CNT2,
  72. .int_msk = VID_B_INT_MSK,
  73. .int_stat = VID_B_INT_STAT,
  74. .int_mstat = VID_B_INT_MSTAT,
  75. .dma_ctl = VID_DST_B_DMA_CTL,
  76. .gpcnt_ctl = VID_DST_B_GPCNT_CTL,
  77. .gpcnt = VID_DST_B_GPCNT,
  78. .vip_ctl = VID_DST_B_VIP_CTL,
  79. .pix_frmt = VID_DST_B_PIX_FRMT,
  80. },
  81. [SRAM_CH02] = {
  82. .i = SRAM_CH02,
  83. .name = "VID C",
  84. .cmds_start = VID_C_DOWN_CMDS,
  85. .ctrl_start = VID_C_IQ,
  86. .cdt = VID_C_CDT,
  87. .fifo_start = VID_C_DOWN_CLUSTER_1,
  88. .fifo_size = (VID_CLUSTER_SIZE << 2),
  89. .ptr1_reg = DMA3_PTR1,
  90. .ptr2_reg = DMA3_PTR2,
  91. .cnt1_reg = DMA3_CNT1,
  92. .cnt2_reg = DMA3_CNT2,
  93. .int_msk = VID_C_INT_MSK,
  94. .int_stat = VID_C_INT_STAT,
  95. .int_mstat = VID_C_INT_MSTAT,
  96. .dma_ctl = VID_DST_C_DMA_CTL,
  97. .gpcnt_ctl = VID_DST_C_GPCNT_CTL,
  98. .gpcnt = VID_DST_C_GPCNT,
  99. .vip_ctl = VID_DST_C_VIP_CTL,
  100. .pix_frmt = VID_DST_C_PIX_FRMT,
  101. },
  102. [SRAM_CH03] = {
  103. .i = SRAM_CH03,
  104. .name = "VID D",
  105. .cmds_start = VID_D_DOWN_CMDS,
  106. .ctrl_start = VID_D_IQ,
  107. .cdt = VID_D_CDT,
  108. .fifo_start = VID_D_DOWN_CLUSTER_1,
  109. .fifo_size = (VID_CLUSTER_SIZE << 2),
  110. .ptr1_reg = DMA4_PTR1,
  111. .ptr2_reg = DMA4_PTR2,
  112. .cnt1_reg = DMA4_CNT1,
  113. .cnt2_reg = DMA4_CNT2,
  114. .int_msk = VID_D_INT_MSK,
  115. .int_stat = VID_D_INT_STAT,
  116. .int_mstat = VID_D_INT_MSTAT,
  117. .dma_ctl = VID_DST_D_DMA_CTL,
  118. .gpcnt_ctl = VID_DST_D_GPCNT_CTL,
  119. .gpcnt = VID_DST_D_GPCNT,
  120. .vip_ctl = VID_DST_D_VIP_CTL,
  121. .pix_frmt = VID_DST_D_PIX_FRMT,
  122. },
  123. [SRAM_CH04] = {
  124. .i = SRAM_CH04,
  125. .name = "VID E",
  126. .cmds_start = VID_E_DOWN_CMDS,
  127. .ctrl_start = VID_E_IQ,
  128. .cdt = VID_E_CDT,
  129. .fifo_start = VID_E_DOWN_CLUSTER_1,
  130. .fifo_size = (VID_CLUSTER_SIZE << 2),
  131. .ptr1_reg = DMA5_PTR1,
  132. .ptr2_reg = DMA5_PTR2,
  133. .cnt1_reg = DMA5_CNT1,
  134. .cnt2_reg = DMA5_CNT2,
  135. .int_msk = VID_E_INT_MSK,
  136. .int_stat = VID_E_INT_STAT,
  137. .int_mstat = VID_E_INT_MSTAT,
  138. .dma_ctl = VID_DST_E_DMA_CTL,
  139. .gpcnt_ctl = VID_DST_E_GPCNT_CTL,
  140. .gpcnt = VID_DST_E_GPCNT,
  141. .vip_ctl = VID_DST_E_VIP_CTL,
  142. .pix_frmt = VID_DST_E_PIX_FRMT,
  143. },
  144. [SRAM_CH05] = {
  145. .i = SRAM_CH05,
  146. .name = "VID F",
  147. .cmds_start = VID_F_DOWN_CMDS,
  148. .ctrl_start = VID_F_IQ,
  149. .cdt = VID_F_CDT,
  150. .fifo_start = VID_F_DOWN_CLUSTER_1,
  151. .fifo_size = (VID_CLUSTER_SIZE << 2),
  152. .ptr1_reg = DMA6_PTR1,
  153. .ptr2_reg = DMA6_PTR2,
  154. .cnt1_reg = DMA6_CNT1,
  155. .cnt2_reg = DMA6_CNT2,
  156. .int_msk = VID_F_INT_MSK,
  157. .int_stat = VID_F_INT_STAT,
  158. .int_mstat = VID_F_INT_MSTAT,
  159. .dma_ctl = VID_DST_F_DMA_CTL,
  160. .gpcnt_ctl = VID_DST_F_GPCNT_CTL,
  161. .gpcnt = VID_DST_F_GPCNT,
  162. .vip_ctl = VID_DST_F_VIP_CTL,
  163. .pix_frmt = VID_DST_F_PIX_FRMT,
  164. },
  165. [SRAM_CH06] = {
  166. .i = SRAM_CH06,
  167. .name = "VID G",
  168. .cmds_start = VID_G_DOWN_CMDS,
  169. .ctrl_start = VID_G_IQ,
  170. .cdt = VID_G_CDT,
  171. .fifo_start = VID_G_DOWN_CLUSTER_1,
  172. .fifo_size = (VID_CLUSTER_SIZE << 2),
  173. .ptr1_reg = DMA7_PTR1,
  174. .ptr2_reg = DMA7_PTR2,
  175. .cnt1_reg = DMA7_CNT1,
  176. .cnt2_reg = DMA7_CNT2,
  177. .int_msk = VID_G_INT_MSK,
  178. .int_stat = VID_G_INT_STAT,
  179. .int_mstat = VID_G_INT_MSTAT,
  180. .dma_ctl = VID_DST_G_DMA_CTL,
  181. .gpcnt_ctl = VID_DST_G_GPCNT_CTL,
  182. .gpcnt = VID_DST_G_GPCNT,
  183. .vip_ctl = VID_DST_G_VIP_CTL,
  184. .pix_frmt = VID_DST_G_PIX_FRMT,
  185. },
  186. [SRAM_CH07] = {
  187. .i = SRAM_CH07,
  188. .name = "VID H",
  189. .cmds_start = VID_H_DOWN_CMDS,
  190. .ctrl_start = VID_H_IQ,
  191. .cdt = VID_H_CDT,
  192. .fifo_start = VID_H_DOWN_CLUSTER_1,
  193. .fifo_size = (VID_CLUSTER_SIZE << 2),
  194. .ptr1_reg = DMA8_PTR1,
  195. .ptr2_reg = DMA8_PTR2,
  196. .cnt1_reg = DMA8_CNT1,
  197. .cnt2_reg = DMA8_CNT2,
  198. .int_msk = VID_H_INT_MSK,
  199. .int_stat = VID_H_INT_STAT,
  200. .int_mstat = VID_H_INT_MSTAT,
  201. .dma_ctl = VID_DST_H_DMA_CTL,
  202. .gpcnt_ctl = VID_DST_H_GPCNT_CTL,
  203. .gpcnt = VID_DST_H_GPCNT,
  204. .vip_ctl = VID_DST_H_VIP_CTL,
  205. .pix_frmt = VID_DST_H_PIX_FRMT,
  206. },
  207. [SRAM_CH08] = {
  208. .name = "audio from",
  209. .cmds_start = AUD_A_DOWN_CMDS,
  210. .ctrl_start = AUD_A_IQ,
  211. .cdt = AUD_A_CDT,
  212. .fifo_start = AUD_A_DOWN_CLUSTER_1,
  213. .fifo_size = AUDIO_CLUSTER_SIZE * 3,
  214. .ptr1_reg = DMA17_PTR1,
  215. .ptr2_reg = DMA17_PTR2,
  216. .cnt1_reg = DMA17_CNT1,
  217. .cnt2_reg = DMA17_CNT2,
  218. },
  219. [SRAM_CH09] = {
  220. .i = SRAM_CH09,
  221. .name = "VID Upstream I",
  222. .cmds_start = VID_I_UP_CMDS,
  223. .ctrl_start = VID_I_IQ,
  224. .cdt = VID_I_CDT,
  225. .fifo_start = VID_I_UP_CLUSTER_1,
  226. .fifo_size = (VID_CLUSTER_SIZE << 2),
  227. .ptr1_reg = DMA15_PTR1,
  228. .ptr2_reg = DMA15_PTR2,
  229. .cnt1_reg = DMA15_CNT1,
  230. .cnt2_reg = DMA15_CNT2,
  231. .int_msk = VID_I_INT_MSK,
  232. .int_stat = VID_I_INT_STAT,
  233. .int_mstat = VID_I_INT_MSTAT,
  234. .dma_ctl = VID_SRC_I_DMA_CTL,
  235. .gpcnt_ctl = VID_SRC_I_GPCNT_CTL,
  236. .gpcnt = VID_SRC_I_GPCNT,
  237. .vid_fmt_ctl = VID_SRC_I_FMT_CTL,
  238. .vid_active_ctl1 = VID_SRC_I_ACTIVE_CTL1,
  239. .vid_active_ctl2 = VID_SRC_I_ACTIVE_CTL2,
  240. .vid_cdt_size = VID_SRC_I_CDT_SZ,
  241. .irq_bit = 8,
  242. },
  243. [SRAM_CH10] = {
  244. .i = SRAM_CH10,
  245. .name = "VID Upstream J",
  246. .cmds_start = VID_J_UP_CMDS,
  247. .ctrl_start = VID_J_IQ,
  248. .cdt = VID_J_CDT,
  249. .fifo_start = VID_J_UP_CLUSTER_1,
  250. .fifo_size = (VID_CLUSTER_SIZE << 2),
  251. .ptr1_reg = DMA16_PTR1,
  252. .ptr2_reg = DMA16_PTR2,
  253. .cnt1_reg = DMA16_CNT1,
  254. .cnt2_reg = DMA16_CNT2,
  255. .int_msk = VID_J_INT_MSK,
  256. .int_stat = VID_J_INT_STAT,
  257. .int_mstat = VID_J_INT_MSTAT,
  258. .dma_ctl = VID_SRC_J_DMA_CTL,
  259. .gpcnt_ctl = VID_SRC_J_GPCNT_CTL,
  260. .gpcnt = VID_SRC_J_GPCNT,
  261. .vid_fmt_ctl = VID_SRC_J_FMT_CTL,
  262. .vid_active_ctl1 = VID_SRC_J_ACTIVE_CTL1,
  263. .vid_active_ctl2 = VID_SRC_J_ACTIVE_CTL2,
  264. .vid_cdt_size = VID_SRC_J_CDT_SZ,
  265. .irq_bit = 9,
  266. },
  267. [SRAM_CH11] = {
  268. .i = SRAM_CH11,
  269. .name = "Audio Upstream Channel B",
  270. .cmds_start = AUD_B_UP_CMDS,
  271. .ctrl_start = AUD_B_IQ,
  272. .cdt = AUD_B_CDT,
  273. .fifo_start = AUD_B_UP_CLUSTER_1,
  274. .fifo_size = (AUDIO_CLUSTER_SIZE * 3),
  275. .ptr1_reg = DMA22_PTR1,
  276. .ptr2_reg = DMA22_PTR2,
  277. .cnt1_reg = DMA22_CNT1,
  278. .cnt2_reg = DMA22_CNT2,
  279. .int_msk = AUD_B_INT_MSK,
  280. .int_stat = AUD_B_INT_STAT,
  281. .int_mstat = AUD_B_INT_MSTAT,
  282. .dma_ctl = AUD_INT_DMA_CTL,
  283. .gpcnt_ctl = AUD_B_GPCNT_CTL,
  284. .gpcnt = AUD_B_GPCNT,
  285. .aud_length = AUD_B_LNGTH,
  286. .aud_cfg = AUD_B_CFG,
  287. .fld_aud_fifo_en = FLD_AUD_SRC_B_FIFO_EN,
  288. .fld_aud_risc_en = FLD_AUD_SRC_B_RISC_EN,
  289. .irq_bit = 11,
  290. },
  291. };
  292. EXPORT_SYMBOL(cx25821_sram_channels);
  293. static int cx25821_risc_decode(u32 risc)
  294. {
  295. static const char * const instr[16] = {
  296. [RISC_SYNC >> 28] = "sync",
  297. [RISC_WRITE >> 28] = "write",
  298. [RISC_WRITEC >> 28] = "writec",
  299. [RISC_READ >> 28] = "read",
  300. [RISC_READC >> 28] = "readc",
  301. [RISC_JUMP >> 28] = "jump",
  302. [RISC_SKIP >> 28] = "skip",
  303. [RISC_WRITERM >> 28] = "writerm",
  304. [RISC_WRITECM >> 28] = "writecm",
  305. [RISC_WRITECR >> 28] = "writecr",
  306. };
  307. static const int incr[16] = {
  308. [RISC_WRITE >> 28] = 3,
  309. [RISC_JUMP >> 28] = 3,
  310. [RISC_SKIP >> 28] = 1,
  311. [RISC_SYNC >> 28] = 1,
  312. [RISC_WRITERM >> 28] = 3,
  313. [RISC_WRITECM >> 28] = 3,
  314. [RISC_WRITECR >> 28] = 4,
  315. };
  316. static const char * const bits[] = {
  317. "12", "13", "14", "resync",
  318. "cnt0", "cnt1", "18", "19",
  319. "20", "21", "22", "23",
  320. "irq1", "irq2", "eol", "sol",
  321. };
  322. int i;
  323. pr_cont("0x%08x [ %s",
  324. risc, instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
  325. for (i = ARRAY_SIZE(bits) - 1; i >= 0; i--) {
  326. if (risc & (1 << (i + 12)))
  327. pr_cont(" %s", bits[i]);
  328. }
  329. pr_cont(" count=%d ]\n", risc & 0xfff);
  330. return incr[risc >> 28] ? incr[risc >> 28] : 1;
  331. }
  332. static inline int i2c_slave_did_ack(struct i2c_adapter *i2c_adap)
  333. {
  334. struct cx25821_i2c *bus = i2c_adap->algo_data;
  335. struct cx25821_dev *dev = bus->dev;
  336. return cx_read(bus->reg_stat) & 0x01;
  337. }
  338. static void cx25821_registers_init(struct cx25821_dev *dev)
  339. {
  340. u32 tmp;
  341. /* enable RUN_RISC in Pecos */
  342. cx_write(DEV_CNTRL2, 0x20);
  343. /* Set the master PCI interrupt masks to enable video, audio, MBIF,
  344. * and GPIO interrupts
  345. * I2C interrupt masking is handled by the I2C objects themselves. */
  346. cx_write(PCI_INT_MSK, 0x2001FFFF);
  347. tmp = cx_read(RDR_TLCTL0);
  348. tmp &= ~FLD_CFG_RCB_CK_EN; /* Clear the RCB_CK_EN bit */
  349. cx_write(RDR_TLCTL0, tmp);
  350. /* PLL-A setting for the Audio Master Clock */
  351. cx_write(PLL_A_INT_FRAC, 0x9807A58B);
  352. /* PLL_A_POST = 0x1C, PLL_A_OUT_TO_PIN = 0x1 */
  353. cx_write(PLL_A_POST_STAT_BIST, 0x8000019C);
  354. /* clear reset bit [31] */
  355. tmp = cx_read(PLL_A_INT_FRAC);
  356. cx_write(PLL_A_INT_FRAC, tmp & 0x7FFFFFFF);
  357. /* PLL-B setting for Mobilygen Host Bus Interface */
  358. cx_write(PLL_B_INT_FRAC, 0x9883A86F);
  359. /* PLL_B_POST = 0xD, PLL_B_OUT_TO_PIN = 0x0 */
  360. cx_write(PLL_B_POST_STAT_BIST, 0x8000018D);
  361. /* clear reset bit [31] */
  362. tmp = cx_read(PLL_B_INT_FRAC);
  363. cx_write(PLL_B_INT_FRAC, tmp & 0x7FFFFFFF);
  364. /* PLL-C setting for video upstream channel */
  365. cx_write(PLL_C_INT_FRAC, 0x96A0EA3F);
  366. /* PLL_C_POST = 0x3, PLL_C_OUT_TO_PIN = 0x0 */
  367. cx_write(PLL_C_POST_STAT_BIST, 0x80000103);
  368. /* clear reset bit [31] */
  369. tmp = cx_read(PLL_C_INT_FRAC);
  370. cx_write(PLL_C_INT_FRAC, tmp & 0x7FFFFFFF);
  371. /* PLL-D setting for audio upstream channel */
  372. cx_write(PLL_D_INT_FRAC, 0x98757F5B);
  373. /* PLL_D_POST = 0x13, PLL_D_OUT_TO_PIN = 0x0 */
  374. cx_write(PLL_D_POST_STAT_BIST, 0x80000113);
  375. /* clear reset bit [31] */
  376. tmp = cx_read(PLL_D_INT_FRAC);
  377. cx_write(PLL_D_INT_FRAC, tmp & 0x7FFFFFFF);
  378. /* This selects the PLL C clock source for the video upstream channel
  379. * I and J */
  380. tmp = cx_read(VID_CH_CLK_SEL);
  381. cx_write(VID_CH_CLK_SEL, (tmp & 0x00FFFFFF) | 0x24000000);
  382. /* 656/VIP SRC Upstream Channel I & J and 7 - Host Bus Interface for
  383. * channel A-C
  384. * select 656/VIP DST for downstream Channel A - C */
  385. tmp = cx_read(VID_CH_MODE_SEL);
  386. /* cx_write( VID_CH_MODE_SEL, tmp | 0x1B0001FF); */
  387. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  388. /* enables 656 port I and J as output */
  389. tmp = cx_read(CLK_RST);
  390. /* use external ALT_PLL_REF pin as its reference clock instead */
  391. tmp |= FLD_USE_ALT_PLL_REF;
  392. cx_write(CLK_RST, tmp & ~(FLD_VID_I_CLK_NOE | FLD_VID_J_CLK_NOE));
  393. mdelay(100);
  394. }
  395. int cx25821_sram_channel_setup(struct cx25821_dev *dev,
  396. const struct sram_channel *ch,
  397. unsigned int bpl, u32 risc)
  398. {
  399. unsigned int i, lines;
  400. u32 cdt;
  401. if (ch->cmds_start == 0) {
  402. cx_write(ch->ptr1_reg, 0);
  403. cx_write(ch->ptr2_reg, 0);
  404. cx_write(ch->cnt2_reg, 0);
  405. cx_write(ch->cnt1_reg, 0);
  406. return 0;
  407. }
  408. bpl = (bpl + 7) & ~7; /* alignment */
  409. cdt = ch->cdt;
  410. lines = ch->fifo_size / bpl;
  411. if (lines > 4)
  412. lines = 4;
  413. BUG_ON(lines < 2);
  414. cx_write(8 + 0, RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  415. cx_write(8 + 4, 8);
  416. cx_write(8 + 8, 0);
  417. /* write CDT */
  418. for (i = 0; i < lines; i++) {
  419. cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
  420. cx_write(cdt + 16 * i + 4, 0);
  421. cx_write(cdt + 16 * i + 8, 0);
  422. cx_write(cdt + 16 * i + 12, 0);
  423. }
  424. /* init the first cdt buffer */
  425. for (i = 0; i < 128; i++)
  426. cx_write(ch->fifo_start + 4 * i, i);
  427. /* write CMDS */
  428. if (ch->jumponly)
  429. cx_write(ch->cmds_start + 0, 8);
  430. else
  431. cx_write(ch->cmds_start + 0, risc);
  432. cx_write(ch->cmds_start + 4, 0); /* 64 bits 63-32 */
  433. cx_write(ch->cmds_start + 8, cdt);
  434. cx_write(ch->cmds_start + 12, (lines * 16) >> 3);
  435. cx_write(ch->cmds_start + 16, ch->ctrl_start);
  436. if (ch->jumponly)
  437. cx_write(ch->cmds_start + 20, 0x80000000 | (64 >> 2));
  438. else
  439. cx_write(ch->cmds_start + 20, 64 >> 2);
  440. for (i = 24; i < 80; i += 4)
  441. cx_write(ch->cmds_start + i, 0);
  442. /* fill registers */
  443. cx_write(ch->ptr1_reg, ch->fifo_start);
  444. cx_write(ch->ptr2_reg, cdt);
  445. cx_write(ch->cnt2_reg, (lines * 16) >> 3);
  446. cx_write(ch->cnt1_reg, (bpl >> 3) - 1);
  447. return 0;
  448. }
  449. int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
  450. const struct sram_channel *ch,
  451. unsigned int bpl, u32 risc)
  452. {
  453. unsigned int i, lines;
  454. u32 cdt;
  455. if (ch->cmds_start == 0) {
  456. cx_write(ch->ptr1_reg, 0);
  457. cx_write(ch->ptr2_reg, 0);
  458. cx_write(ch->cnt2_reg, 0);
  459. cx_write(ch->cnt1_reg, 0);
  460. return 0;
  461. }
  462. bpl = (bpl + 7) & ~7; /* alignment */
  463. cdt = ch->cdt;
  464. lines = ch->fifo_size / bpl;
  465. if (lines > 3)
  466. lines = 3; /* for AUDIO */
  467. BUG_ON(lines < 2);
  468. cx_write(8 + 0, RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  469. cx_write(8 + 4, 8);
  470. cx_write(8 + 8, 0);
  471. /* write CDT */
  472. for (i = 0; i < lines; i++) {
  473. cx_write(cdt + 16 * i, ch->fifo_start + bpl * i);
  474. cx_write(cdt + 16 * i + 4, 0);
  475. cx_write(cdt + 16 * i + 8, 0);
  476. cx_write(cdt + 16 * i + 12, 0);
  477. }
  478. /* write CMDS */
  479. if (ch->jumponly)
  480. cx_write(ch->cmds_start + 0, 8);
  481. else
  482. cx_write(ch->cmds_start + 0, risc);
  483. cx_write(ch->cmds_start + 4, 0); /* 64 bits 63-32 */
  484. cx_write(ch->cmds_start + 8, cdt);
  485. cx_write(ch->cmds_start + 12, (lines * 16) >> 3);
  486. cx_write(ch->cmds_start + 16, ch->ctrl_start);
  487. /* IQ size */
  488. if (ch->jumponly)
  489. cx_write(ch->cmds_start + 20, 0x80000000 | (64 >> 2));
  490. else
  491. cx_write(ch->cmds_start + 20, 64 >> 2);
  492. /* zero out */
  493. for (i = 24; i < 80; i += 4)
  494. cx_write(ch->cmds_start + i, 0);
  495. /* fill registers */
  496. cx_write(ch->ptr1_reg, ch->fifo_start);
  497. cx_write(ch->ptr2_reg, cdt);
  498. cx_write(ch->cnt2_reg, (lines * 16) >> 3);
  499. cx_write(ch->cnt1_reg, (bpl >> 3) - 1);
  500. return 0;
  501. }
  502. EXPORT_SYMBOL(cx25821_sram_channel_setup_audio);
  503. void cx25821_sram_channel_dump(struct cx25821_dev *dev, const struct sram_channel *ch)
  504. {
  505. static char *name[] = {
  506. "init risc lo",
  507. "init risc hi",
  508. "cdt base",
  509. "cdt size",
  510. "iq base",
  511. "iq size",
  512. "risc pc lo",
  513. "risc pc hi",
  514. "iq wr ptr",
  515. "iq rd ptr",
  516. "cdt current",
  517. "pci target lo",
  518. "pci target hi",
  519. "line / byte",
  520. };
  521. u32 risc;
  522. unsigned int i, j, n;
  523. pr_warn("%s: %s - dma channel status dump\n", dev->name, ch->name);
  524. for (i = 0; i < ARRAY_SIZE(name); i++)
  525. pr_warn("cmds + 0x%2x: %-15s: 0x%08x\n",
  526. i * 4, name[i], cx_read(ch->cmds_start + 4 * i));
  527. j = i * 4;
  528. for (i = 0; i < 4;) {
  529. risc = cx_read(ch->cmds_start + 4 * (i + 14));
  530. pr_warn("cmds + 0x%2x: risc%d: ", j + i * 4, i);
  531. i += cx25821_risc_decode(risc);
  532. }
  533. for (i = 0; i < (64 >> 2); i += n) {
  534. risc = cx_read(ch->ctrl_start + 4 * i);
  535. /* No consideration for bits 63-32 */
  536. pr_warn("ctrl + 0x%2x (0x%08x): iq %x: ",
  537. i * 4, ch->ctrl_start + 4 * i, i);
  538. n = cx25821_risc_decode(risc);
  539. for (j = 1; j < n; j++) {
  540. risc = cx_read(ch->ctrl_start + 4 * (i + j));
  541. pr_warn("ctrl + 0x%2x : iq %x: 0x%08x [ arg #%d ]\n",
  542. 4 * (i + j), i + j, risc, j);
  543. }
  544. }
  545. pr_warn(" : fifo: 0x%08x -> 0x%x\n",
  546. ch->fifo_start, ch->fifo_start + ch->fifo_size);
  547. pr_warn(" : ctrl: 0x%08x -> 0x%x\n",
  548. ch->ctrl_start, ch->ctrl_start + 6 * 16);
  549. pr_warn(" : ptr1_reg: 0x%08x\n",
  550. cx_read(ch->ptr1_reg));
  551. pr_warn(" : ptr2_reg: 0x%08x\n",
  552. cx_read(ch->ptr2_reg));
  553. pr_warn(" : cnt1_reg: 0x%08x\n",
  554. cx_read(ch->cnt1_reg));
  555. pr_warn(" : cnt2_reg: 0x%08x\n",
  556. cx_read(ch->cnt2_reg));
  557. }
  558. void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
  559. const struct sram_channel *ch)
  560. {
  561. static const char * const name[] = {
  562. "init risc lo",
  563. "init risc hi",
  564. "cdt base",
  565. "cdt size",
  566. "iq base",
  567. "iq size",
  568. "risc pc lo",
  569. "risc pc hi",
  570. "iq wr ptr",
  571. "iq rd ptr",
  572. "cdt current",
  573. "pci target lo",
  574. "pci target hi",
  575. "line / byte",
  576. };
  577. u32 risc, value, tmp;
  578. unsigned int i, j, n;
  579. pr_info("\n%s: %s - dma Audio channel status dump\n",
  580. dev->name, ch->name);
  581. for (i = 0; i < ARRAY_SIZE(name); i++)
  582. pr_info("%s: cmds + 0x%2x: %-15s: 0x%08x\n",
  583. dev->name, i * 4, name[i],
  584. cx_read(ch->cmds_start + 4 * i));
  585. j = i * 4;
  586. for (i = 0; i < 4;) {
  587. risc = cx_read(ch->cmds_start + 4 * (i + 14));
  588. pr_warn("cmds + 0x%2x: risc%d: ", j + i * 4, i);
  589. i += cx25821_risc_decode(risc);
  590. }
  591. for (i = 0; i < (64 >> 2); i += n) {
  592. risc = cx_read(ch->ctrl_start + 4 * i);
  593. /* No consideration for bits 63-32 */
  594. pr_warn("ctrl + 0x%2x (0x%08x): iq %x: ",
  595. i * 4, ch->ctrl_start + 4 * i, i);
  596. n = cx25821_risc_decode(risc);
  597. for (j = 1; j < n; j++) {
  598. risc = cx_read(ch->ctrl_start + 4 * (i + j));
  599. pr_warn("ctrl + 0x%2x : iq %x: 0x%08x [ arg #%d ]\n",
  600. 4 * (i + j), i + j, risc, j);
  601. }
  602. }
  603. pr_warn(" : fifo: 0x%08x -> 0x%x\n",
  604. ch->fifo_start, ch->fifo_start + ch->fifo_size);
  605. pr_warn(" : ctrl: 0x%08x -> 0x%x\n",
  606. ch->ctrl_start, ch->ctrl_start + 6 * 16);
  607. pr_warn(" : ptr1_reg: 0x%08x\n",
  608. cx_read(ch->ptr1_reg));
  609. pr_warn(" : ptr2_reg: 0x%08x\n",
  610. cx_read(ch->ptr2_reg));
  611. pr_warn(" : cnt1_reg: 0x%08x\n",
  612. cx_read(ch->cnt1_reg));
  613. pr_warn(" : cnt2_reg: 0x%08x\n",
  614. cx_read(ch->cnt2_reg));
  615. for (i = 0; i < 4; i++) {
  616. risc = cx_read(ch->cmds_start + 56 + (i * 4));
  617. pr_warn("instruction %d = 0x%x\n", i, risc);
  618. }
  619. /* read data from the first cdt buffer */
  620. risc = cx_read(AUD_A_CDT);
  621. pr_warn("\nread cdt loc=0x%x\n", risc);
  622. for (i = 0; i < 8; i++) {
  623. n = cx_read(risc + i * 4);
  624. pr_cont("0x%x ", n);
  625. }
  626. pr_cont("\n\n");
  627. value = cx_read(CLK_RST);
  628. CX25821_INFO(" CLK_RST = 0x%x\n\n", value);
  629. value = cx_read(PLL_A_POST_STAT_BIST);
  630. CX25821_INFO(" PLL_A_POST_STAT_BIST = 0x%x\n\n", value);
  631. value = cx_read(PLL_A_INT_FRAC);
  632. CX25821_INFO(" PLL_A_INT_FRAC = 0x%x\n\n", value);
  633. value = cx_read(PLL_B_POST_STAT_BIST);
  634. CX25821_INFO(" PLL_B_POST_STAT_BIST = 0x%x\n\n", value);
  635. value = cx_read(PLL_B_INT_FRAC);
  636. CX25821_INFO(" PLL_B_INT_FRAC = 0x%x\n\n", value);
  637. value = cx_read(PLL_C_POST_STAT_BIST);
  638. CX25821_INFO(" PLL_C_POST_STAT_BIST = 0x%x\n\n", value);
  639. value = cx_read(PLL_C_INT_FRAC);
  640. CX25821_INFO(" PLL_C_INT_FRAC = 0x%x\n\n", value);
  641. value = cx_read(PLL_D_POST_STAT_BIST);
  642. CX25821_INFO(" PLL_D_POST_STAT_BIST = 0x%x\n\n", value);
  643. value = cx_read(PLL_D_INT_FRAC);
  644. CX25821_INFO(" PLL_D_INT_FRAC = 0x%x\n\n", value);
  645. value = cx25821_i2c_read(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL, &tmp);
  646. CX25821_INFO(" AFE_AB_DIAG_CTRL (0x10900090) = 0x%x\n\n", value);
  647. }
  648. EXPORT_SYMBOL(cx25821_sram_channel_dump_audio);
  649. static void cx25821_shutdown(struct cx25821_dev *dev)
  650. {
  651. int i;
  652. /* disable RISC controller */
  653. cx_write(DEV_CNTRL2, 0);
  654. /* Disable Video A/B activity */
  655. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  656. cx_write(dev->channels[i].sram_channels->dma_ctl, 0);
  657. cx_write(dev->channels[i].sram_channels->int_msk, 0);
  658. }
  659. for (i = VID_UPSTREAM_SRAM_CHANNEL_I;
  660. i <= VID_UPSTREAM_SRAM_CHANNEL_J; i++) {
  661. cx_write(dev->channels[i].sram_channels->dma_ctl, 0);
  662. cx_write(dev->channels[i].sram_channels->int_msk, 0);
  663. }
  664. /* Disable Audio activity */
  665. cx_write(AUD_INT_DMA_CTL, 0);
  666. /* Disable Serial port */
  667. cx_write(UART_CTL, 0);
  668. /* Disable Interrupts */
  669. cx_write(PCI_INT_MSK, 0);
  670. cx_write(AUD_A_INT_MSK, 0);
  671. }
  672. void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel_select,
  673. u32 format)
  674. {
  675. if (channel_select <= 7 && channel_select >= 0) {
  676. cx_write(dev->channels[channel_select].sram_channels->pix_frmt,
  677. format);
  678. }
  679. dev->channels[channel_select].pixel_formats = format;
  680. }
  681. static void cx25821_set_vip_mode(struct cx25821_dev *dev,
  682. const struct sram_channel *ch)
  683. {
  684. cx_write(ch->pix_frmt, PIXEL_FRMT_422);
  685. cx_write(ch->vip_ctl, PIXEL_ENGINE_VIP1);
  686. }
  687. static void cx25821_initialize(struct cx25821_dev *dev)
  688. {
  689. int i;
  690. dprintk(1, "%s()\n", __func__);
  691. cx25821_shutdown(dev);
  692. cx_write(PCI_INT_STAT, 0xffffffff);
  693. for (i = 0; i < VID_CHANNEL_NUM; i++)
  694. cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
  695. cx_write(AUD_A_INT_STAT, 0xffffffff);
  696. cx_write(AUD_B_INT_STAT, 0xffffffff);
  697. cx_write(AUD_C_INT_STAT, 0xffffffff);
  698. cx_write(AUD_D_INT_STAT, 0xffffffff);
  699. cx_write(AUD_E_INT_STAT, 0xffffffff);
  700. cx_write(CLK_DELAY, cx_read(CLK_DELAY) & 0x80000000);
  701. cx_write(PAD_CTRL, 0x12); /* for I2C */
  702. cx25821_registers_init(dev); /* init Pecos registers */
  703. mdelay(100);
  704. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  705. cx25821_set_vip_mode(dev, dev->channels[i].sram_channels);
  706. cx25821_sram_channel_setup(dev, dev->channels[i].sram_channels,
  707. 1440, 0);
  708. dev->channels[i].pixel_formats = PIXEL_FRMT_422;
  709. dev->channels[i].use_cif_resolution = 0;
  710. }
  711. /* Probably only affect Downstream */
  712. for (i = VID_UPSTREAM_SRAM_CHANNEL_I;
  713. i <= VID_UPSTREAM_SRAM_CHANNEL_J; i++) {
  714. dev->channels[i].pixel_formats = PIXEL_FRMT_422;
  715. cx25821_set_vip_mode(dev, dev->channels[i].sram_channels);
  716. }
  717. cx25821_sram_channel_setup_audio(dev,
  718. dev->channels[SRAM_CH08].sram_channels, 128, 0);
  719. cx25821_gpio_init(dev);
  720. }
  721. static int cx25821_get_resources(struct cx25821_dev *dev)
  722. {
  723. if (request_mem_region(pci_resource_start(dev->pci, 0),
  724. pci_resource_len(dev->pci, 0), dev->name))
  725. return 0;
  726. pr_err("%s: can't get MMIO memory @ 0x%llx\n",
  727. dev->name, (unsigned long long)pci_resource_start(dev->pci, 0));
  728. return -EBUSY;
  729. }
  730. static void cx25821_dev_checkrevision(struct cx25821_dev *dev)
  731. {
  732. dev->hwrevision = cx_read(RDR_CFG2) & 0xff;
  733. pr_info("Hardware revision = 0x%02x\n", dev->hwrevision);
  734. }
  735. static void cx25821_iounmap(struct cx25821_dev *dev)
  736. {
  737. if (dev == NULL)
  738. return;
  739. /* Releasing IO memory */
  740. if (dev->lmmio != NULL) {
  741. iounmap(dev->lmmio);
  742. dev->lmmio = NULL;
  743. }
  744. }
  745. static int cx25821_dev_setup(struct cx25821_dev *dev)
  746. {
  747. static unsigned int cx25821_devcount;
  748. int i;
  749. mutex_init(&dev->lock);
  750. dev->nr = ++cx25821_devcount;
  751. sprintf(dev->name, "cx25821[%d]", dev->nr);
  752. if (dev->nr >= ARRAY_SIZE(card)) {
  753. CX25821_INFO("dev->nr >= %zd", ARRAY_SIZE(card));
  754. return -ENODEV;
  755. }
  756. if (dev->pci->device != 0x8210) {
  757. pr_info("%s(): Exiting. Incorrect Hardware device = 0x%02x\n",
  758. __func__, dev->pci->device);
  759. return -ENODEV;
  760. }
  761. pr_info("Athena Hardware device = 0x%02x\n", dev->pci->device);
  762. /* Apply a sensible clock frequency for the PCIe bridge */
  763. dev->clk_freq = 28000000;
  764. for (i = 0; i < MAX_VID_CHANNEL_NUM; i++) {
  765. dev->channels[i].dev = dev;
  766. dev->channels[i].id = i;
  767. dev->channels[i].sram_channels = &cx25821_sram_channels[i];
  768. }
  769. /* board config */
  770. dev->board = 1; /* card[dev->nr]; */
  771. dev->_max_num_decoders = MAX_DECODERS;
  772. dev->pci_bus = dev->pci->bus->number;
  773. dev->pci_slot = PCI_SLOT(dev->pci->devfn);
  774. dev->pci_irqmask = 0x001f00;
  775. /* External Master 1 Bus */
  776. dev->i2c_bus[0].nr = 0;
  777. dev->i2c_bus[0].dev = dev;
  778. dev->i2c_bus[0].reg_stat = I2C1_STAT;
  779. dev->i2c_bus[0].reg_ctrl = I2C1_CTRL;
  780. dev->i2c_bus[0].reg_addr = I2C1_ADDR;
  781. dev->i2c_bus[0].reg_rdata = I2C1_RDATA;
  782. dev->i2c_bus[0].reg_wdata = I2C1_WDATA;
  783. dev->i2c_bus[0].i2c_period = (0x07 << 24); /* 1.95MHz */
  784. if (cx25821_get_resources(dev) < 0) {
  785. pr_err("%s: No more PCIe resources for subsystem: %04x:%04x\n",
  786. dev->name, dev->pci->subsystem_vendor,
  787. dev->pci->subsystem_device);
  788. cx25821_devcount--;
  789. return -EBUSY;
  790. }
  791. /* PCIe stuff */
  792. dev->base_io_addr = pci_resource_start(dev->pci, 0);
  793. if (!dev->base_io_addr) {
  794. CX25821_ERR("No PCI Memory resources, exiting!\n");
  795. return -ENODEV;
  796. }
  797. dev->lmmio = ioremap(dev->base_io_addr, pci_resource_len(dev->pci, 0));
  798. if (!dev->lmmio) {
  799. CX25821_ERR("ioremap failed, maybe increasing __VMALLOC_RESERVE in page.h\n");
  800. cx25821_iounmap(dev);
  801. return -ENOMEM;
  802. }
  803. dev->bmmio = (u8 __iomem *) dev->lmmio;
  804. pr_info("%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
  805. dev->name, dev->pci->subsystem_vendor,
  806. dev->pci->subsystem_device, cx25821_boards[dev->board].name,
  807. dev->board, card[dev->nr] == dev->board ?
  808. "insmod option" : "autodetected");
  809. /* init hardware */
  810. cx25821_initialize(dev);
  811. cx25821_i2c_register(&dev->i2c_bus[0]);
  812. /* cx25821_i2c_register(&dev->i2c_bus[1]);
  813. * cx25821_i2c_register(&dev->i2c_bus[2]); */
  814. if (medusa_video_init(dev) < 0)
  815. CX25821_ERR("%s(): Failed to initialize medusa!\n", __func__);
  816. cx25821_video_register(dev);
  817. cx25821_dev_checkrevision(dev);
  818. return 0;
  819. }
  820. void cx25821_dev_unregister(struct cx25821_dev *dev)
  821. {
  822. int i;
  823. if (!dev->base_io_addr)
  824. return;
  825. release_mem_region(dev->base_io_addr, pci_resource_len(dev->pci, 0));
  826. for (i = 0; i < MAX_VID_CAP_CHANNEL_NUM - 1; i++) {
  827. if (i == SRAM_CH08) /* audio channel */
  828. continue;
  829. /*
  830. * TODO: enable when video output is properly
  831. * supported.
  832. if (i == SRAM_CH09 || i == SRAM_CH10)
  833. cx25821_free_mem_upstream(&dev->channels[i]);
  834. */
  835. cx25821_video_unregister(dev, i);
  836. }
  837. cx25821_i2c_unregister(&dev->i2c_bus[0]);
  838. cx25821_iounmap(dev);
  839. }
  840. EXPORT_SYMBOL(cx25821_dev_unregister);
  841. int cx25821_riscmem_alloc(struct pci_dev *pci,
  842. struct cx25821_riscmem *risc,
  843. unsigned int size)
  844. {
  845. __le32 *cpu;
  846. dma_addr_t dma = 0;
  847. if (NULL != risc->cpu && risc->size < size)
  848. pci_free_consistent(pci, risc->size, risc->cpu, risc->dma);
  849. if (NULL == risc->cpu) {
  850. cpu = pci_zalloc_consistent(pci, size, &dma);
  851. if (NULL == cpu)
  852. return -ENOMEM;
  853. risc->cpu = cpu;
  854. risc->dma = dma;
  855. risc->size = size;
  856. }
  857. return 0;
  858. }
  859. EXPORT_SYMBOL(cx25821_riscmem_alloc);
  860. static __le32 *cx25821_risc_field(__le32 * rp, struct scatterlist *sglist,
  861. unsigned int offset, u32 sync_line,
  862. unsigned int bpl, unsigned int padding,
  863. unsigned int lines, bool jump)
  864. {
  865. struct scatterlist *sg;
  866. unsigned int line, todo;
  867. if (jump) {
  868. *(rp++) = cpu_to_le32(RISC_JUMP);
  869. *(rp++) = cpu_to_le32(0);
  870. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  871. }
  872. /* sync instruction */
  873. if (sync_line != NO_SYNC_LINE)
  874. *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
  875. /* scan lines */
  876. sg = sglist;
  877. for (line = 0; line < lines; line++) {
  878. while (offset && offset >= sg_dma_len(sg)) {
  879. offset -= sg_dma_len(sg);
  880. sg = sg_next(sg);
  881. }
  882. if (bpl <= sg_dma_len(sg) - offset) {
  883. /* fits into current chunk */
  884. *(rp++) = cpu_to_le32(RISC_WRITE | RISC_SOL | RISC_EOL |
  885. bpl);
  886. *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
  887. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  888. offset += bpl;
  889. } else {
  890. /* scanline needs to be split */
  891. todo = bpl;
  892. *(rp++) = cpu_to_le32(RISC_WRITE | RISC_SOL |
  893. (sg_dma_len(sg) - offset));
  894. *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
  895. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  896. todo -= (sg_dma_len(sg) - offset);
  897. offset = 0;
  898. sg = sg_next(sg);
  899. while (todo > sg_dma_len(sg)) {
  900. *(rp++) = cpu_to_le32(RISC_WRITE |
  901. sg_dma_len(sg));
  902. *(rp++) = cpu_to_le32(sg_dma_address(sg));
  903. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  904. todo -= sg_dma_len(sg);
  905. sg = sg_next(sg);
  906. }
  907. *(rp++) = cpu_to_le32(RISC_WRITE | RISC_EOL | todo);
  908. *(rp++) = cpu_to_le32(sg_dma_address(sg));
  909. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  910. offset += todo;
  911. }
  912. offset += padding;
  913. }
  914. return rp;
  915. }
  916. int cx25821_risc_buffer(struct pci_dev *pci, struct cx25821_riscmem *risc,
  917. struct scatterlist *sglist, unsigned int top_offset,
  918. unsigned int bottom_offset, unsigned int bpl,
  919. unsigned int padding, unsigned int lines)
  920. {
  921. u32 instructions;
  922. u32 fields;
  923. __le32 *rp;
  924. int rc;
  925. fields = 0;
  926. if (UNSET != top_offset)
  927. fields++;
  928. if (UNSET != bottom_offset)
  929. fields++;
  930. /* estimate risc mem: worst case is one write per page border +
  931. one write per scan line + syncs + jump (all 3 dwords). Padding
  932. can cause next bpl to start close to a page border. First DMA
  933. region may be smaller than PAGE_SIZE */
  934. /* write and jump need and extra dword */
  935. instructions = fields * (1 + ((bpl + padding) * lines) / PAGE_SIZE +
  936. lines);
  937. instructions += 5;
  938. rc = cx25821_riscmem_alloc(pci, risc, instructions * 12);
  939. if (rc < 0)
  940. return rc;
  941. /* write risc instructions */
  942. rp = risc->cpu;
  943. if (UNSET != top_offset) {
  944. rp = cx25821_risc_field(rp, sglist, top_offset, 0, bpl, padding,
  945. lines, true);
  946. }
  947. if (UNSET != bottom_offset) {
  948. rp = cx25821_risc_field(rp, sglist, bottom_offset, 0x200, bpl,
  949. padding, lines, UNSET == top_offset);
  950. }
  951. /* save pointer to jmp instruction address */
  952. risc->jmp = rp;
  953. BUG_ON((risc->jmp - risc->cpu + 3) * sizeof(*risc->cpu) > risc->size);
  954. return 0;
  955. }
  956. static __le32 *cx25821_risc_field_audio(__le32 * rp, struct scatterlist *sglist,
  957. unsigned int offset, u32 sync_line,
  958. unsigned int bpl, unsigned int padding,
  959. unsigned int lines, unsigned int lpi)
  960. {
  961. struct scatterlist *sg;
  962. unsigned int line, todo, sol;
  963. /* sync instruction */
  964. if (sync_line != NO_SYNC_LINE)
  965. *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
  966. /* scan lines */
  967. sg = sglist;
  968. for (line = 0; line < lines; line++) {
  969. while (offset && offset >= sg_dma_len(sg)) {
  970. offset -= sg_dma_len(sg);
  971. sg = sg_next(sg);
  972. }
  973. if (lpi && line > 0 && !(line % lpi))
  974. sol = RISC_SOL | RISC_IRQ1 | RISC_CNT_INC;
  975. else
  976. sol = RISC_SOL;
  977. if (bpl <= sg_dma_len(sg) - offset) {
  978. /* fits into current chunk */
  979. *(rp++) = cpu_to_le32(RISC_WRITE | sol | RISC_EOL |
  980. bpl);
  981. *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
  982. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  983. offset += bpl;
  984. } else {
  985. /* scanline needs to be split */
  986. todo = bpl;
  987. *(rp++) = cpu_to_le32(RISC_WRITE | sol |
  988. (sg_dma_len(sg) - offset));
  989. *(rp++) = cpu_to_le32(sg_dma_address(sg) + offset);
  990. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  991. todo -= (sg_dma_len(sg) - offset);
  992. offset = 0;
  993. sg = sg_next(sg);
  994. while (todo > sg_dma_len(sg)) {
  995. *(rp++) = cpu_to_le32(RISC_WRITE |
  996. sg_dma_len(sg));
  997. *(rp++) = cpu_to_le32(sg_dma_address(sg));
  998. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  999. todo -= sg_dma_len(sg);
  1000. sg = sg_next(sg);
  1001. }
  1002. *(rp++) = cpu_to_le32(RISC_WRITE | RISC_EOL | todo);
  1003. *(rp++) = cpu_to_le32(sg_dma_address(sg));
  1004. *(rp++) = cpu_to_le32(0); /* bits 63-32 */
  1005. offset += todo;
  1006. }
  1007. offset += padding;
  1008. }
  1009. return rp;
  1010. }
  1011. int cx25821_risc_databuffer_audio(struct pci_dev *pci,
  1012. struct cx25821_riscmem *risc,
  1013. struct scatterlist *sglist,
  1014. unsigned int bpl,
  1015. unsigned int lines, unsigned int lpi)
  1016. {
  1017. u32 instructions;
  1018. __le32 *rp;
  1019. int rc;
  1020. /* estimate risc mem: worst case is one write per page border +
  1021. one write per scan line + syncs + jump (all 2 dwords). Here
  1022. there is no padding and no sync. First DMA region may be smaller
  1023. than PAGE_SIZE */
  1024. /* Jump and write need an extra dword */
  1025. instructions = 1 + (bpl * lines) / PAGE_SIZE + lines;
  1026. instructions += 1;
  1027. rc = cx25821_riscmem_alloc(pci, risc, instructions * 12);
  1028. if (rc < 0)
  1029. return rc;
  1030. /* write risc instructions */
  1031. rp = risc->cpu;
  1032. rp = cx25821_risc_field_audio(rp, sglist, 0, NO_SYNC_LINE, bpl, 0,
  1033. lines, lpi);
  1034. /* save pointer to jmp instruction address */
  1035. risc->jmp = rp;
  1036. BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
  1037. return 0;
  1038. }
  1039. EXPORT_SYMBOL(cx25821_risc_databuffer_audio);
  1040. void cx25821_free_buffer(struct cx25821_dev *dev, struct cx25821_buffer *buf)
  1041. {
  1042. BUG_ON(in_interrupt());
  1043. if (WARN_ON(buf->risc.size == 0))
  1044. return;
  1045. pci_free_consistent(dev->pci,
  1046. buf->risc.size, buf->risc.cpu, buf->risc.dma);
  1047. memset(&buf->risc, 0, sizeof(buf->risc));
  1048. }
  1049. static irqreturn_t cx25821_irq(int irq, void *dev_id)
  1050. {
  1051. struct cx25821_dev *dev = dev_id;
  1052. u32 pci_status;
  1053. u32 vid_status;
  1054. int i, handled = 0;
  1055. u32 mask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
  1056. pci_status = cx_read(PCI_INT_STAT);
  1057. if (pci_status == 0)
  1058. goto out;
  1059. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  1060. if (pci_status & mask[i]) {
  1061. vid_status = cx_read(dev->channels[i].
  1062. sram_channels->int_stat);
  1063. if (vid_status)
  1064. handled += cx25821_video_irq(dev, i,
  1065. vid_status);
  1066. cx_write(PCI_INT_STAT, mask[i]);
  1067. }
  1068. }
  1069. out:
  1070. return IRQ_RETVAL(handled);
  1071. }
  1072. void cx25821_print_irqbits(char *name, char *tag, char **strings,
  1073. int len, u32 bits, u32 mask)
  1074. {
  1075. unsigned int i;
  1076. printk(KERN_DEBUG pr_fmt("%s: %s [0x%x]"), name, tag, bits);
  1077. for (i = 0; i < len; i++) {
  1078. if (!(bits & (1 << i)))
  1079. continue;
  1080. if (strings[i])
  1081. pr_cont(" %s", strings[i]);
  1082. else
  1083. pr_cont(" %d", i);
  1084. if (!(mask & (1 << i)))
  1085. continue;
  1086. pr_cont("*");
  1087. }
  1088. pr_cont("\n");
  1089. }
  1090. EXPORT_SYMBOL(cx25821_print_irqbits);
  1091. struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci)
  1092. {
  1093. struct cx25821_dev *dev = pci_get_drvdata(pci);
  1094. return dev;
  1095. }
  1096. EXPORT_SYMBOL(cx25821_dev_get);
  1097. static int cx25821_initdev(struct pci_dev *pci_dev,
  1098. const struct pci_device_id *pci_id)
  1099. {
  1100. struct cx25821_dev *dev;
  1101. int err = 0;
  1102. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1103. if (NULL == dev)
  1104. return -ENOMEM;
  1105. err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
  1106. if (err < 0)
  1107. goto fail_free;
  1108. /* pci init */
  1109. dev->pci = pci_dev;
  1110. if (pci_enable_device(pci_dev)) {
  1111. err = -EIO;
  1112. pr_info("pci enable failed!\n");
  1113. goto fail_unregister_device;
  1114. }
  1115. err = cx25821_dev_setup(dev);
  1116. if (err)
  1117. goto fail_unregister_pci;
  1118. /* print pci info */
  1119. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  1120. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1121. pr_info("%s/0: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
  1122. dev->name, pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1123. dev->pci_lat, (unsigned long long)dev->base_io_addr);
  1124. pci_set_master(pci_dev);
  1125. err = pci_set_dma_mask(pci_dev, 0xffffffff);
  1126. if (err) {
  1127. pr_err("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
  1128. err = -EIO;
  1129. goto fail_irq;
  1130. }
  1131. err = request_irq(pci_dev->irq, cx25821_irq,
  1132. IRQF_SHARED, dev->name, dev);
  1133. if (err < 0) {
  1134. pr_err("%s: can't get IRQ %d\n", dev->name, pci_dev->irq);
  1135. goto fail_irq;
  1136. }
  1137. return 0;
  1138. fail_irq:
  1139. pr_info("cx25821_initdev() can't get IRQ !\n");
  1140. cx25821_dev_unregister(dev);
  1141. fail_unregister_pci:
  1142. pci_disable_device(pci_dev);
  1143. fail_unregister_device:
  1144. v4l2_device_unregister(&dev->v4l2_dev);
  1145. fail_free:
  1146. kfree(dev);
  1147. return err;
  1148. }
  1149. static void cx25821_finidev(struct pci_dev *pci_dev)
  1150. {
  1151. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  1152. struct cx25821_dev *dev = get_cx25821(v4l2_dev);
  1153. cx25821_shutdown(dev);
  1154. pci_disable_device(pci_dev);
  1155. /* unregister stuff */
  1156. if (pci_dev->irq)
  1157. free_irq(pci_dev->irq, dev);
  1158. cx25821_dev_unregister(dev);
  1159. v4l2_device_unregister(v4l2_dev);
  1160. kfree(dev);
  1161. }
  1162. static const struct pci_device_id cx25821_pci_tbl[] = {
  1163. {
  1164. /* CX25821 Athena */
  1165. .vendor = 0x14f1,
  1166. .device = 0x8210,
  1167. .subvendor = 0x14f1,
  1168. .subdevice = 0x0920,
  1169. }, {
  1170. /* CX25821 No Brand */
  1171. .vendor = 0x14f1,
  1172. .device = 0x8210,
  1173. .subvendor = 0x0000,
  1174. .subdevice = 0x0000,
  1175. }, {
  1176. /* --- end of list --- */
  1177. }
  1178. };
  1179. MODULE_DEVICE_TABLE(pci, cx25821_pci_tbl);
  1180. static struct pci_driver cx25821_pci_driver = {
  1181. .name = "cx25821",
  1182. .id_table = cx25821_pci_tbl,
  1183. .probe = cx25821_initdev,
  1184. .remove = cx25821_finidev,
  1185. /* TODO */
  1186. .suspend = NULL,
  1187. .resume = NULL,
  1188. };
  1189. static int __init cx25821_init(void)
  1190. {
  1191. pr_info("driver version %d.%d.%d loaded\n",
  1192. (CX25821_VERSION_CODE >> 16) & 0xff,
  1193. (CX25821_VERSION_CODE >> 8) & 0xff,
  1194. CX25821_VERSION_CODE & 0xff);
  1195. return pci_register_driver(&cx25821_pci_driver);
  1196. }
  1197. static void __exit cx25821_fini(void)
  1198. {
  1199. pci_unregister_driver(&cx25821_pci_driver);
  1200. }
  1201. module_init(cx25821_init);
  1202. module_exit(cx25821_fini);